Sunday, 28 December 2014

Cpp Program to find Factorial of a given number

This is a Cpp program which calculates the factorial of a given number.

Generally to calculate the factorial of  a given number 'n' we calculate the product of all the numbers less than or equal to 'n'.

For Example : 5! = 5*4*3*2*1
                             =  120


PROGRAM :  
#include <ostream>

using namespace std;

int factorial(int x)
{
    if(x==1)
        return 1;
    else
        return x*factorial(x-1);
}

int main()
{
    int n;
    cout << "Enter Number to find it's factorial : \t" ;
    cin >> n;
    cout << "\n The factorial of " << n << " is : "<< factorial(n) << endl;
    return 0;

}
OUTPUT :

Cpp - Factorial