Wednesday, 27 January 2016

Find Factorial of Number Using Recursion in C++

Find Factorial of Number Using Recursion in C++

Hello friends, Hitesh is Here; Today we will learn about how to write Factorial of any number using Recursion in C++. In general factorial of any number is;
For example factorial of 5 is; 5x4x3x2x1= 120

Find Factorial of Number Using Recursion in C++


Factorial Program Code

#include<iostream>
using namespace std;
int factorial(int n);
int main()
 {
 int n; cout << "Enter any positive number: ";
 cin >> n;
 cout << "Factorial of " << n << " : " << factorial(n);
 return 0;
}
 int factorial(int n)
{
 if(n!=1) return n*factorial(n-1);
}

Output:

Enter any positive number: 5
Factorial of 5 : 120

No comments:

Post a Comment