Write a Program in C++ That Do Iterative Function Factorial

// Iterative function factorial.
#include <iostream>
#include <iomanip>
using namespace std;

unsigned long factorial( unsigned int ); // function prototype

int main()
{
   // calculate the factorials of 0 through 10
   for ( unsigned int counter = 0; counter <= 10; ++counter )
      cout << setw( 2 ) << counter << "! = " << factorial( counter ) 
         << endl;
} // end main

// iterative method factorial
unsigned long factorial( unsigned int number )
{
   unsigned long result = 1;

   // iterative factorial calculation 
   for ( unsigned int i = number; i >= 1; --i )
      result *= i;

   return result;
} // end function factorial

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...