Saturday, 17 March 2018

Recursion in C++

The function which is called by itself is called recursion.
OR
When function is called within the same function, it is known as recursion.
Example:
#include<iostream.h>
#include<conio.h>
void main()
{
int fact(int);
int i,f,num;
clrscr();
cout<<"Enter any number: ";
cin>>num;
f=fact(num);
cout<<"Factorial: "<<f;
getch();
}
int fact(int a)
{
if(a<0)
return(-1);
if(a==0)
return(1);
else
{
return(a*fact(a-1));
}
}
Output:
Enter any number:5
Factorial:120

1 comment:

  1. Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.

    ReplyDelete

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...