Calculates factorials, demonstrate FOR loop - C++ Statement

C++ examples for Statement:for

Description

Calculates factorials, demonstrate FOR loop

Demo Code

#include <iostream>
using namespace std;
int main()/*from  w  w  w . ja va2s.  com*/
{
   unsigned int numb;
   unsigned long fact=1;             //long for larger numbers
   cout << "Enter a number: ";
   cin >> numb;                      //get number
   for(int j=numb; j>0; j--)         //multiply 1 by
      fact *= j;                     //numb, numb-1, ..., 2, 1
   cout << "Factorial is " << fact << endl;
   return 0;
}

Result


Related Tutorials