Digital Root
InterviewTime limit1sMemory limit128 MB
Given positive integers up to 1000 digits, each line until a terminating 0, print the digital root of each number.
- Level
Easy3 of 10
- Topics
- Math, Number theory, String, Implementation
- Solved
- No attempts yet
Problem
The digital root of a positive integer is computed as follows. First, add up all of the digits of . If the resulting sum is a single-digit number, that value is the digital root of . If it has two or more digits, add up all of its digits again, and repeat this process until a single-digit number is obtained.
For example, consider the digital root of . Since and is a single digit, the digital root of is . For , we have , which has two digits, so we add its digits once more. This gives , so the digital root is .
Given a positive integer , write a program that computes its digital root.
Input
Positive integers are given one per line. Each integer has at most digits. The last line of the input is , which only marks the end of the input and must not be processed.
Output
For each positive integer in the input, print its digital root on its own line, in the same order as the input.