0
0
DSA Cprogramming~10 mins

Why Recursion Exists and What Loops Cannot Express Cleanly in DSA C - Test Your Knowledge

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
An - 1
Bn + 1
Cn * 2
Dn / 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n * 2 or n / 2 does not count down properly.
2fill in blank
medium

Complete 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'
An * 2
Bn + 1
Cn - 1
Dn / 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n * 2 or n / 2 does not compute factorial correctly.
3fill in blank
hard

Fix 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'
An - 2
Bn - 1
Cn + 1
Dn * 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using n - 1 twice causes incorrect results.
Using n + 1 or n * 2 causes errors or infinite recursion.
4fill in blank
hard

Fill 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'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or division instead of addition.
Not reducing n in the recursive call.
5fill in blank
hard

Fill 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'
An - 1
Bn
Creturn
Dn + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Calling with n + 1 causes infinite recursion.
Not returning after printing causes unexpected behavior.