Problem Statement: Write a program in cpp to find the factorial of a number.
Note:- Scroll horizontally to see the full line of code.
// Program to find the factorial of a number using recursion
#include <iostream>
using namespace std;
int factorial(int n)
{
if (n == 0)
{
return 1;
}
return n * factorial(n - 1);
}
int main()
{
int n;
cout << "Enter n: ";
cin >> n;
cout << "Factorial of " << n << " is " << factorial(n);
cout << endl;
return 0;
}
Comments
Post a Comment