Complete the code to return 1 when n is 0, the base case for factorial.
int factorial(int n) {
if (n == [1]) {
return 1;
}
return n * factorial(n - 1);
}The base case for factorial is when n is 0, returning 1.
Complete the recursive call to compute factorial of n-1.
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial([1]);
}The recursive call reduces n by 1 each time to reach the base case.
Fix the error in the base case condition to correctly stop recursion.
int sumToN(int n) {
if (n [1] 0) {
return 0;
}
return n + sumToN(n - 1);
}The base case should check if n equals 0 to stop recursion.
Fill both blanks to create a recursive function that sums numbers from 1 to n.
int sumToN(int n) {
if (n [1] 0) {
return 0;
}
return n [2] sumToN(n - 1);
}The base case checks if n == 0, and the recursive case adds n to sumToN(n-1).
Fill all three blanks to complete a recursive function that computes the nth Fibonacci number.
int fibonacci(int n) {
if (n [1] 0) {
return 0;
} else if (n [2] 1) {
return 1;
}
return fibonacci(n - 1) [3] fibonacci(n - 2);
}Base cases check if n == 0 or n == 1, and recursive case adds fibonacci(n-1) and fibonacci(n-2).
