RECURSION
Recursion is an approach in which the function definition refers to itself. Each time the function calls itself with the slightly simpler version of the original problem.
PROGRAM
//Program to find factorial of a number using recursion.
# include <iostream>
using namespace std;
int fact(int n)
{
if(n==0) //Base condition
return 1;
else
return n * fact(n-1) ;
}
int main()
{
int num;
cout<<"Enter a number:";
cin>>num;
cout<<"Factorial:"<<fact(num);
return 0;
}
OUTPUT
Recursion is an approach in which the function definition refers to itself. Each time the function calls itself with the slightly simpler version of the original problem.
PROGRAM
//Program to find factorial of a number using recursion.
# include <iostream>
using namespace std;
int fact(int n)
{
if(n==0) //Base condition
return 1;
else
return n * fact(n-1) ;
}
int main()
{
int num;
cout<<"Enter a number:";
cin>>num;
cout<<"Factorial:"<<fact(num);
return 0;
}
OUTPUT