ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
int f(int n) {
    if (n <= 1) return 1;
    return n * f(n - 1);
}
int main() { printf("%d", f(5)); }
  1. A. 120
  2. B. 24
  3. C. 15
  4. D. Infinite recursion

Correct answer: A. 120

This is a standard factorial recursion: f(5)=5*f(4)=5*4*f(3)=...=5*4*3*2*1=120. Each call's multiplication is deferred until the recursive call returns, so the multiplications happen in reverse (unwinding) order but the final product is the same regardless.

More C Programming questions

Exams that ask this topic