Program to find Factorial of a number using recursion in C++
Solution
#include <iostream> using namespace std; int factorial(int n) { if(n==1) return 1; else return n*factorial(n-1); } int main() { int n,ans; cout<<"Enter a number "; cin>>n; ans = factorial(n); cout << "Factorial of "<< n<<" is "<<ans; return 0; }
Output :
Enter a number 5 Factorial of 5 is 120