46. Digital Root
Core1000 ms256 MBSolved by 0%
The digital root is what you get by adding a number's digits, then adding the digits of that result, and repeating until a single digit remains.
For 9875: 9+8+7+5 = 29, then 2+9 = 11, then 1+1 = 2. So the digital root is 2.
Read a non-negative integer and print its digital root.
Constraints - `0 ≤ n ≤ 1000000000000000000`
Input
A single line containing one integer n.
Output
Print one integer between 0 and 9 — the digital root.
Input9875
Output2
Noteis the worked example from the statement
Input0
Output0
Noteis zero, whose digital root is zero — the shortcut formula fails here unless handled
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution