ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int f(int n, int acc) {
    if (n == 0) return acc;
    return f(n - 1, acc * n);
}
int main(void) { printf("%d", f(4, 1)); }
  1. A. 10
  2. B. 24
  3. C. 4
  4. D. 0

Correct answer: B. 24

This is tail-recursive factorial accumulation: f(4,1)=f(3,4)=f(2,12)=f(1,24)=f(0,24)=24.

More C Programming questions

Exams that ask this topic