C++ for statement Calculate factorial of numbers with for loop, no recursive function

Description

C++ for statement Calculate factorial of numbers with for loop, no recursive function

#include <iostream>
using namespace std;
int main()/*from   w  ww  .j  a  va  2s  .c  o  m*/
{
   int outer, num, fact, total;
   cout << "What factorial do you want to see? ";
   cin >> num;
   for (outer=1; outer <= num; outer++)
   {
      total = 1;   // Initialize total for each factorial.
      for (fact=1; fact<= outer; fact++)
      {
         total *= fact;
      }      // Compute each factorial.
   }
   cout << "The factorial for " << num << " is " << total;
   return 0;
}



PreviousNext

Related