ProgrammingModerateTCS NQT · Wipro

What is printed?

What is printed?
#include <stdio.h>
unsigned sumd(unsigned n) {
    return n == 0 ? 0 : n % 10 + sumd(n / 10);
}
int main(void) {
    printf("%u", sumd(98765));
}
  1. A. 29
  2. B. 40
  3. C. 36
  4. D. 35

Correct answer: D. 35

The recursion adds the last digit and recurses on the remaining digits, giving 9 + 8 + 7 + 6 + 5 = 35. Stopping one digit early gives 29.

More C Programming questions

Exams that ask this topic