+92 332 4229 857 99ProjectIdeas@Gmail.com

How to calculate factorial of a number (C++)


Factorial
factorial() calculates the factorial of the given value and it takes 1 parameter:
number takes the value whose factorial is to be determined.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "iomanip"
using namespace std;

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

 for(int i=number;i>0;i--)
  factorial=factorial*i;

return factorial;
}

int main()
{
       cout << factorial ( 0.0 ) << endl ;
       cout << factorial ( 1.0 ) << endl ;
       cout << factorial ( 2.0 ) << endl ;
       cout << factorial ( 3.0 ) << endl ;
       cout << factorial ( 4.0 ) << endl ;
       cout << factorial ( 5.0 ) << endl ;
       cout << factorial ( 6.0 ) << endl ;
       cout << factorial ( 7.0 ) << endl ;
       cout << factorial ( 8.0 ) << endl ;
       cout << factorial ( 9.0 ) << endl ;

       _getche();
       return 0;
}

Output
1
1
2
6
24
120
720
5040
40320
362880

0 comments: