0
0
DSA Cprogramming~20 mins

Factorial Using Recursion in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Factorial Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this factorial recursion code?
Consider the following C code that calculates factorial using recursion. What will be printed when n = 4?
DSA C
int factorial(int n) {
    if (n == 0) {
        return 1;
    }
    return n * factorial(n - 1);
}

int main() {
    int n = 4;
    int result = factorial(n);
    printf("%d", result);
    return 0;
}
A120
B10
C24
D1
Attempts:
2 left
💡 Hint
Factorial of 4 is 4 * 3 * 2 * 1.
Predict Output
intermediate
2:00remaining
What is the output when factorial is called with n = 0?
Look at this factorial function. What will it print when n = 0?
DSA C
int factorial(int n) {
    if (n == 0) {
        return 1;
    }
    return n * factorial(n - 1);
}

int main() {
    int n = 0;
    int result = factorial(n);
    printf("%d", result);
    return 0;
}
A1
B0
CUndefined behavior
DError at runtime
Attempts:
2 left
💡 Hint
Factorial of zero is defined as 1.
🔧 Debug
advanced
2:00remaining
What error occurs when calling factorial(-1)?
Consider this factorial function. What happens if we call factorial(-1)?
DSA C
int factorial(int n) {
    if (n == 0) {
        return 1;
    }
    return n * factorial(n - 1);
}

int main() {
    int n = -1;
    int result = factorial(n);
    printf("%d", result);
    return 0;
}
ACompilation error
BReturns 1 immediately
CReturns 0
DStack overflow due to infinite recursion
Attempts:
2 left
💡 Hint
Check if the base case handles negative numbers.
Predict Output
advanced
2:00remaining
What is the output of this modified factorial function?
What will this code print when n = 3?
DSA C
int factorial(int n) {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

int main() {
    int n = 3;
    int result = factorial(n);
    printf("%d", result);
    return 0;
}
A3
B6
C1
D0
Attempts:
2 left
💡 Hint
Factorial of 3 is 3 * 2 * 1.
🧠 Conceptual
expert
2:00remaining
Why is recursion not always the best choice for factorial calculation?
Which of the following is the main reason recursion might be less efficient than iteration for factorial calculation?
ARecursion uses more memory due to call stack overhead
BRecursion cannot handle large numbers
CRecursion always produces wrong results
DRecursion is slower because it uses loops internally
Attempts:
2 left
💡 Hint
Think about what happens each time a function calls itself.