Computes the factorial of numbers with for loop, no recursive function - C++ Data Type

C++ examples for Data Type:int

Description

Computes the factorial of numbers with for loop, no recursive function

Demo Code

#include <iostream>
using namespace std;
int main()//from   w  w  w  . java 2  s.co 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;
}

Result


Related Tutorials