ProgrammingHardHCL · Tech Mahindra 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(5, 1));
}
  1. A. 1
  2. B. 5
  3. C. 120
  4. D. 24

Correct answer: C. 120

This is tail-recursive factorial using an accumulator: acc carries the running product forward instead of multiplying after the recursive call returns. f(5,1)->f(4,5)->f(3,20)->f(2,60)->f(1,120)->f(0,120), returning 120 immediately at the base case with no further multiplication needed on the way back up.

More C Programming questions

Exams that ask this topic