ProgrammingModerateHCL · Tech Mahindra pattern

What does f(4096) return?

What does f(4096) return?
int f(int n) {
    if (n == 0) return 0;
    return n % 10 + f(n / 10);
}
  1. A. 10
  2. B. 4096
  3. C. 19
  4. D. 15

Correct answer: C. 19

The function adds the last digit and recurses on the rest: 6 + 9 + 0 + 4 = 19, ending when n becomes 0. It is the digit-sum routine.

More C Programming questions

Exams that ask this topic