Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to make the function call itself recursively.
DSA C
void countdown(int n) {
if (n <= 0) {
printf("Blastoff!\n");
return;
}
printf("%d\n", n);
countdown([1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n * 2 or n / 2 does not count down properly.
✗ Incorrect
The function calls itself with n - 1 to count down until it reaches zero.
2fill in blank
mediumComplete the code to calculate factorial using recursion.
DSA C
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial([1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n * 2 or n / 2 does not compute factorial correctly.
✗ Incorrect
Factorial of n is n times factorial of n - 1, reducing n each call.
3fill in blank
hardFix the error in the recursive Fibonacci function call.
DSA C
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci([1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using n - 1 twice causes incorrect results.
Using n + 1 or n * 2 causes errors or infinite recursion.
✗ Incorrect
Fibonacci is sum of fibonacci(n-1) and fibonacci(n-2).
4fill in blank
hardFill both blanks to create a recursive function that sums numbers from n down to 1.
DSA C
int sum_to_n(int n) {
if (n == 0) {
return 0;
}
return n [1] sum_to_n(n [2] 1);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or division instead of addition.
Not reducing n in the recursive call.
✗ Incorrect
Sum is n plus sum of numbers from n-1 down to 1.
5fill in blank
hardFill all three blanks to create a recursive function that prints numbers from 1 to n.
DSA C
void print_ascending(int n) {
if (n == 0) {
return;
}
print_ascending([1]);
printf("%d\n", [2]);
[3];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling with n + 1 causes infinite recursion.
Not returning after printing causes unexpected behavior.
✗ Incorrect
The function calls itself with n-1, then prints n, then returns.