Question: The program below reads a single integer from standard input and then attempts to compute its factorial using a recursive function. For example, if the
The program below reads a single integer from standard input and then attempts to compute its factorial using a recursive function. For example, if the user enters 5, the program should output 120 because 1*2*3*4*5=120. Unfortunately the program has a few bugs.
Find and fix the bugs. The program should be fixable with only small modifications,do not rewrite the entire program.
/* This program should read a single integer from standard input then * compute its factorial. For example, if the user enters 5, the program should * output 120 because 1*2*3*4*5=120. */
#include #include
int factorial(int n) { assert(n>=0 && n int result = 0; if(n == 0) return result; result = factorial(n+1) * result; }
// DO NOT MODIFY THE CODE BELOW
int main() { int n, result;
scanf(\"%d\", &n); result = factorial(n); printf(\"%d \", result); return 0; }
For example:
| Input | Result |
| 5 | 120 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
