Monday 6 August 2012

How to find out the factorial of a number in C++

Program to find out the Factorial of a number



#include<iostream>

#include<conio.h>

using namespace std;

int factorial(int number);


int main()
{
       int result = 0, number = 0;

       cout << "\n\n\t\t\t__ Factorial of a Number __\n";

       cout << "\n\n  Enter the Number  -  ";

       cin >> number;

       result = factorial(number);

       cout << "\n\n\t\t    ______ Factorial of " << number << " is " << result << " ______";

       getch();

       return 0;

}

int factorial(int number)
{
       int factor_result = 1;

       while(number >= 1)
       {
              factor_result = factor_result * number;

              number--;
       }

       return factor_result;
}


Factorial of a number is that number times 1 less than the factorial of that number. For Example -  Factorial of 10 is = 10 X 9!, where 9! will be 9 X 8! and so one until it reaches 1. The factorial of 1 is 1.

No comments:

Post a Comment