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));
}- A. 6
- B. 3
- C. 9
- 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
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 5; printf("%d", (x - 6) > 0); }
- How many inner iterations run in total? Compute with this program. c #include <stdio.h> int main(void) { int i = 0, c = 0; do { for (int j…
- What is the sizeof of this struct, assuming char has size 1, int has size 4 with 4-byte alignment, and a nested struct is laid out in…
- What is printed? c #include <stdio.h> long long sum(long long n, long long acc) { return n == 0 ? acc : sum(n - 1, acc + n); } int…
- How many odd multiples of 3 are there in 1 to 30? Count them with this program. c #include <stdio.h> int main(void) { int c = 0; for…
- What is printed? c #include <stdio.h> int main(void) { int m[2][2] = {{3, 1}, {4, 2}}; int s = 0; for (int i = 0; i < 2; i++) { int lo =…