Recursive Factorial function

#include <iostream.h>
#include <iomanip.h>
 unsigned long factorial( unsigned long );
 int main()
 {
 for ( int i = 0; i <= 10; i++ )
 cout << setw( 2 ) << i << "! = " << factorial( i ) << endl;
 return 0;
 }
unsigned long factorial( unsigned long number )
 {
 if ( number <= 1 ) 
 return 1;
 else 
 return number * factorial( number - 1 );
}

 OUTPUT:
         0! = 1
         1! = 1
         2! = 2
         3! = 6
         4! = 24
         5! = 120
         6! = 720
         7! = 5040
         8! = 40320
         9! = 362880
         10! = 3628800


No comments:

Post a Comment