Complete the code to return 1 when n is 0 in the factorial function.
int factorial(int n) {
if (n == [1]) {
return 1;
}
return n * factorial(n - 1);
}The factorial of 0 is defined as 1, so the base case checks if n is 0.
Complete the recursive call to calculate factorial of n-1.
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial([1]);
}The factorial function calls itself with n-1 to reduce the problem size step by step.
Fix the error in the main function to correctly call factorial and print the result.
#include <stdio.h> int factorial(int n) { if (n == 0) { return 1; } return n * factorial(n - 1); } int main() { int num = 5; int result = factorial([1]); printf("Factorial of %d is %d\n", num, result); return 0; }
The factorial function should be called with the variable num to calculate factorial of 5.
Fill both blanks to complete the factorial function with proper base case and recursive call.
int factorial(int n) {
if (n == [1]) {
return [2];
}
return n * factorial(n - 1);
}The base case checks if n is 0 and returns 1, which is the factorial of 0.
Fill all three blanks to create a recursive factorial function with base case and recursive step.
int factorial(int [1]) { if ([2] == 0) { return [3]; } return [2] * factorial([2] - 1); }
The function parameter is n, the base case checks if n is 0, and returns 1 as factorial of 0.