In ancient Europe, people believed that their luck was dependent on a number. By summing up the digits of their birthday, they got a sum. By repeatedly adding the digits of the sum until a single digit number remains. This resultant number was called the "single digit representation". And the digit reflected their luck in their whole life.
In this question, a birthday will be given by a non-negative integer (
digits). Your program has to compute the single digit representation of the given number. Example:
Input Specification
The first input is an integer specifying the number of test cases. Then each input number appears on a line by itself.
Output Specification
For each test case, output the single digit representation of it.
Sample Input
4
1
10
19
999
Sample Output
1
1
1
9
Comments
you want to use mod 9 to handle bigger numbers. after the first inital sum of the digits, you want to mod 9 the result and if mod9==0 then it means that the final sum will be 9, ex: 12345, 1+2+3+4+5=15, 1+5=6 which is the same as 12345%9=6, so 0 means its 9 and anything else will be equal to the mod aka the answer. sorry if explanation is muddy.
For Python users, start with this code:
It makes it much faster.
This kind of number is better known as "digital root" (999, anyone?)
Why do I keep getting IRs?
The input integers can be up to
digits long.
.
Integer.parseInt()
cannot parse integers from the input greater thanAnd in fact you cannot store a
digit number as an
int
or even along
. Consider storing it as aString
.This comment is hidden due to too much negative feedback. Show it anyway.
You're trying to read an integer of maximum length
digits. That's far greater than the
max value of an
int
.oh. I didn't know that. Thank you!