ProgrammingHardProduct company OA

What is printed?

What is printed?
int dr(int n) { return n < 10 ? n : dr(n / 10 + n % 10); }
int main(void) { printf("%d %d", dr(9875), dr(99999)); }
  1. A. 11 18
  2. B. 2 9
  3. C. 29 45
  4. D. 2 0

Correct answer: B. 2 9

Replacing n by n / 10 + n % 10 never changes n modulo 9, so the recursion ends at the digital root. 9875: 992 → 101 → 11 → 2. 99999: 10008 → 1008 → 108 → 18 → 9 (a multiple of 9 ends at 9, not 0). '29 45' are the plain digit sums.

More C Programming questions

Exams that ask this topic