0
0
DSA Cprogramming~10 mins

Fibonacci Using Recursion 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 Fibonacci recursion.

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

Complete the code to define the second base case for Fibonacci recursion.

DSA C
int fibonacci(int n) {
    if (n == 0) {
        return 0;
    } else if (n == [1]) {
        return 1;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}
Drag options to blanks, or click blank then click option'
A2
B3
C-1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for the second base case.
Missing the second base case causing incorrect results.
3fill in blank
hard

Fix the error in the recursive call to correctly calculate Fibonacci.

DSA C
int fibonacci(int n) {
    if (n == 0) {
        return 0;
    } else if (n == 1) {
        return 1;
    }
    return fibonacci(n [1] 1) + fibonacci(n - 2);
}
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - causing infinite recursion.
Using * or / which are incorrect for Fibonacci.
4fill in blank
hard

Fill both blanks to complete the main function that prints Fibonacci numbers up to n.

DSA C
#include <stdio.h>

int fibonacci(int n);

int main() {
    int n = 5;
    for (int i = [1]; i [2] n; i++) {
        printf("%d ", fibonacci(i));
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A0
B<=
C<
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop at 1 missing the first Fibonacci number.
Using <= causing one extra number to print.
5fill in blank
hard

Fill all three blanks to complete the recursive Fibonacci function with correct base cases and recursive calls.

DSA C
int fibonacci(int n) {
    if (n == [1]) {
        return 0;
    } else if (n == [2]) {
        return 1;
    }
    return fibonacci(n [3] 1) + fibonacci(n - 2);
}
Drag options to blanks, or click blank then click option'
A0
B1
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up base case values.
Using addition instead of subtraction in recursive calls.