Question: Write a C program that uses a for loop to compute n factorial (n is a positive integer). Test your program using various n. Write
Write a C program that uses a for loop to compute n factorial (n is a positive integer). Test your program using various n. Write a main function to call the following recursive function we studied in class.
int factorial( int num )
{
if ( num == 0 || num == 1 )
return 1;
return num * factorial( num 1 );
}
Rewrite the recursive function above to track the execution of a recursive function. Use it to compute 6! and observe the execution process.
int factorial( int num )
{
int tmp;
printf(num = %d when entering recursion , num);
if ( num == 0 || num == 1 ){
printf(factorial = 1 when returning at num = =%d , num);
return 1;
}
tmp=num * factorial( num 1 );
printf(factorial = %d when returning at num = =%d , tmp, num);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
