Complete the code to define a recursive function that prints numbers from n down to 1.
void printNumbers(int n) {
if (n == 0) return;
std::cout << n << " ";
printNumbers([1]);
}The function calls itself with n - 1 to count down from n to 1 recursively.
Complete the code to calculate factorial of n recursively.
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial([1]);
}Factorial of n is n times factorial of n-1, reducing the problem size each call.
Fix the error in the recursive Fibonacci function call.
int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib([1]);
}Fibonacci is sum of fib(n-1) and fib(n-2), so the second call must be fib(n-2).
Fill both blanks to create a recursive function that sums elements of an array.
int sumArray(int arr[], int n) {
if (n == 0) return 0;
return arr[[1]] + sumArray(arr, [2]);
}The function sums the last element arr[n-1] plus sum of first n-1 elements recursively.
Fill all three blanks to create a recursive function that reverses a string in place.
void reverseString(char str[], int start, int end) {
if (start >= end) return;
char temp = str[[1]];
str[[2]] = str[[3]];
str[end] = temp;
reverseString(str, start + 1, end - 1);
}The function swaps characters at start and end indices, then recurses inward.
