0
0
DSA Cprogramming~10 mins

Factorial 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 return 1 when n is 0 in 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'
A1
B0
C-1
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 as the base case condition.
Not having a base case causing infinite recursion.
2fill in blank
medium

Complete the recursive call to calculate factorial of n-1.

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 - 1
Bn + 1
Cn
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causing infinite recursion.
Using n causing no progress towards base case.
3fill in blank
hard

Fix the error in the main function to correctly call factorial and print the result.

DSA C
#include <stdio.h>

int factorial(int n) {
    if (n == 0) {
        return 1;
    }
    return n * factorial(n - 1);
}

int main() {
    int num = 5;
    int result = factorial([1]);
    printf("Factorial of %d is %d\n", num, result);
    return 0;
}
Drag options to blanks, or click blank then click option'
Anum
Bresult
C5
Dfactorial
Attempts:
3 left
💡 Hint
Common Mistakes
Passing result instead of num to factorial.
Passing the function name factorial instead of a number.
4fill in blank
hard

Fill both blanks to complete the factorial function with proper base case and recursive call.

DSA C
int factorial(int n) {
    if (n == [1]) {
        return [2];
    }
    return n * factorial(n - 1);
}
Drag options to blanks, or click blank then click option'
A0
B1
Cn
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Returning n instead of 1 in base case.
Checking for n == 1 instead of n == 0.
5fill in blank
hard

Fill all three blanks to create a recursive factorial function with base case and recursive step.

DSA C
int factorial(int [1]) {
    if ([2] == 0) {
        return [3];
    }
    return [2] * factorial([2] - 1);
}
Drag options to blanks, or click blank then click option'
An
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Returning 0 instead of 1 in base case.