Step-by-step explanation:
Recursion is when the function calls itself inside it's definition.The advantages of recursion are as following:-
- The problems solved by recursion have small code and elegant as compared to it's iterative solution which will be big and ugly to look at.
- Recursion is best suitable for data structures such as trees it's solution more understandable and easy while it's iterative solution is very big and also a bit difficult.
#include<iostream>
using namespace std;
int factorial( int n1)
{
if (n1 == 0)
return 1;
return n1 * factorial(n1 - 1);
}
int main()
{
int no ;
cin>>no;
cout<<factorial(no)<<endl;
return 0;
}
Recursive function to find the factorial of a number.