ProgrammingHardTCS NQT · Infosys

A recursive function sums decimal digits: int ds(int n) { return n < 10 ? n : n % 10 + ds(n / 10); }. What is ds(98765)?

  1. A. 29
  2. B. 41
  3. C. 35
  4. D. 26

Correct answer: C. 35

The digits are 9, 8, 7, 6 and 5, which sum to 35. The recursion goes one level per digit, so its depth is the number of digits.

More C Programming questions

Exams that ask this topic