/* * Program demonstrating implementing a function * that generates the factorial * both iteratively and recursively. * CISC105 06.20.05 */ #include /* * Precondition: n must be positive * n must be small enough so that the factorial is not * bigger than a double or you'll get weird results. * (What is the maximum that n can be?) * Returns the value of n! */ double fact1( int n ) { double value = 1; int i; for( i=1; i <= n; i++ ) { value *= i; } return value; } /* * Precondition: n must be positive * n must be small enough so that the factorial is not * bigger than a double. * (What is the maximum that n can be?) * Returns the value of n! */ double fact2( int n ) { printf("Calling fact2 with parameter %d \n", n ); if( n > 1 ) { return n * fact2( n-1 ); } else { return 1; } } int main() { int number; printf("Give me a number: "); scanf("%d", &number); printf("%d! = %lf \n", number, fact1( number ) ); printf("%d! = %lf \n", number, fact2( number ) ); return 0; }