ProgrammingVery hardCompetitive coding (CP)

What is the digital root of 987654 (the repeated digit sum until one digit remains)? The recursion computes it. What is printed?

What is the digital root of 987654 (the repeated digit sum until one digit remains)? The recursion computes it. What is printed?
#include <stdio.h>
int ds(int n) {
    int s = 0;
    while (n) { s += n % 10; n /= 10; }
    return s;
}
int droot(int n) {
    return n < 10 ? n : droot(ds(n));
}
int main(void) {
    printf("%d", droot(987654));
}
  1. A. 6
  2. B. 3
  3. C. 9
  4. D. 4

Correct answer: B. 3

The digit sums go 987654 to 39, then to 12, then to 3. The digital root of any number is its value modulo 9, and 987654 mod 9 = 3.

More C Programming questions

Exams that ask this topic