0
0
DSA Cprogramming~10 mins

Recursion Base Case and Recursive Case in DSA C - Interactive Practice

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

Complete the code to define the base case for the factorial function.

DSA C
int factorial(int n) {
    if (n == [1]) {
        return 1;
    }
    return n * factorial(n - 1);
}
Drag options to blanks, or click blank then click option'
A0
B1
C-1
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using n == 1 as base case instead of n == 0
Not defining a base case causing infinite recursion
2fill in blank
medium

Complete the code to correctly call the recursive case in the factorial function.

DSA C
int factorial(int n) {
    if (n == 0) {
        return 1;
    }
    return n [1] factorial(n - 1);
}
Drag options to blanks, or click blank then click option'
A/
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication
Using division or subtraction incorrectly
3fill in blank
hard

Fix the error in the recursive Fibonacci function base case.

DSA C
int fibonacci(int n) {
    if (n == [1] || n == 1) {
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}
Drag options to blanks, or click blank then click option'
A1
B2
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using n == 2 as base case
Missing the base case for n == 0
4fill in blank
hard

Fill both blanks to complete the recursive sum function that sums numbers from n down to 1.

DSA C
int sum(int n) {
    if (n [1] 0) {
        return 0;
    }
    return n [2] sum(n - 1);
}
Drag options to blanks, or click blank then click option'
A<=
B>
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator in base case
Using subtraction instead of addition in recursive case
5fill in blank
hard

Fill all three blanks to complete the recursive function that counts down from n to 1 and prints each number.

DSA C
void countdown(int n) {
    if (n [1] 0) {
        return;
    }
    printf("%d\n", n);
    countdown(n [2] 1);
    printf("[3]\n");
}
Drag options to blanks, or click blank then click option'
A<=
B-
C+
DDone
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator in base case
Using addition instead of subtraction in recursive call
Not printing the completion message