Recursion In C++
The process in which a
function calls itself is known as recursion and the corresponding function is
called the recursive function.
Fig: Recursion In C++ |
Program:
#include<iostream.h>
int f(int n)
{
if(n<=1)
return 1;
else
return n*f(n-1);
}
int main()
{
int num;
cout<<”enter a
number”;
cin>>num;
cout<<”factorial of
entered number:”<<f(num);
return 0;
}
Output:
Enter a number : 5
Factorial of entered number
: 120
No comments:
Post a Comment