For each number, print the sum of its digits and its digital root. The digital root is what you get by summing the digits repeatedly until one digit is left. For example, 9875 → 29 → 11 → 2.
The numbers can have up to 1000 digits, so they do not fit in any built-in integer type in C++ or JavaScript. Treat them as strings.
Constraints
- 1 ≤ T ≤ 500
- Each number has 1 to 1000 digits; the whole input is at most 20000 characters.
Input and output
- Input
- First line: T. Then T lines, each a non-negative integer without leading zeros (except the number 0 itself).
- Output
- T lines: `digit_sum digital_root`.
Examples
Input 1
3
9875
0
999Output
29 2
0 0
27 99875: the digits add up to 29 → 11 → 2. 999: 27 → 9.
Input 2
1
123456789123456789123456789Output
135 9Far too big for a 64-bit integer: the digit sum is 135 and the digital root is 9.
Show hints
- Add
ch - '0'for each character. - A number and its digit sum leave the same remainder when divided by 9. That gives a shortcut for the digital root.
Show the explanation
Digit sum: loop over the characters and add c - '0'. That is O(number of digits) and needs no big-integer arithmetic.
Digital root without repeated summing. Since 10 ≡ 1 (mod 9), every power of 10 is ≡ 1 (mod 9). So a number is congruent to its digit sum modulo 9, and so is every later digit sum. The digital root of n is therefore:
- 0 if n = 0;
- otherwise
1 + (n − 1) % 9, which maps multiples of 9 to 9 instead of 0.
Apply this to the digit sum (at most 9000 here, which fits easily) instead of the huge number.
Why interviewers like it: it tests whether you notice that the input does not fit in a 64-bit integer. The largest unsigned 64-bit value has only 20 digits. It also rewards a little number theory, the "casting out nines" rule behind the divisibility-by-9 test. Python could convert the string with int(), but working digit by digit is simpler and works the same in every language.
Original UniCircle question. Last reviewed 22 Oct 2026.
