Find the Digital Root of a Number
Reduce a number to a single digit by repeatedly summing its digits.
Sum the digits of the number. If the result has more than one digit, sum its digits again, and keep going until a single digit remains — that is the digital root. There is also a closed-form shortcut: for n > 0 the digital root equals 1 + (n - 1) % 9.
Problem Statement
The digital root of a non-negative integer is the single digit you reach by
repeatedly summing its digits. For example 9875 -> 9+8+7+5 = 29 -> 2+9 = 11
-> 1+1 = 2, so its digital root is 2.
Given an integer, return its digital root. Do not convert the number to a
String.
Input: A single non-negative integer n.
Output: A single digit (0-9) — the digital root of n.
Examples
Input: 9875
Output: 29+8+7+5 = 29, then 2+9 = 11, then 1+1 = 2.
Input: 12345
Output: 61+2+3+4+5 = 15, then 1+5 = 6.
Constraints
0 <= n <= 2,147,483,647No String conversion
Think Before You Code
Reveal the questions to ask yourself first
- What is the difference between a single digit sum and a repeated digit sum?
- When do you stop repeating the summation?
- Could an inner loop (digit sum) sit inside an outer loop (repeat until one digit)?
- Is there a mathematical shortcut that avoids looping at all?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Reduce the number until only one digit is left.
- While
value >= 10:- Compute the digit sum of
valuewith an inner% 10// 10loop. - Replace
valuewith that digit sum.
- Compute the digit sum of
- When
valuedrops below 10, it is the digital root — return it.
Each outer pass strictly shrinks the number (a multi-digit number's digit sum is always smaller than the number), so the loop is guaranteed to terminate at a single digit.
Dry Run
Walk through the example step by step
Digital root of n = 9875:
pass | value | digit sum
-----+-------+----------
1 | 9875 | 9+8+7+5 = 29
2 | 29 | 2+9 = 11
3 | 11 | 1+1 = 2
end | 2 | < 10 -> digital root = 2
Solution
Reveal the full Java solution
public class DigitalRoot {
public static int digitalRoot(int n) {
int value = Math.abs(n);
while (value >= 10) {
int sum = 0;
while (value != 0) {
sum += value % 10;
value /= 10;
}
value = sum;
}
return value;
}
public static void main(String[] args) {
System.out.println(digitalRoot(9875)); // 2
System.out.println(digitalRoot(12345)); // 6
}
}
The outer loop keeps folding the number until a single digit remains, and the
inner loop is the ordinary digit-sum routine. Because summing the digits of any
number with two or more digits always yields something strictly smaller, the
value monotonically decreases and the process cannot run forever. The same
answer drops out of the formula 1 + (n - 1) % 9 for n > 0, which is why
digital roots underpin the "casting out nines" divisibility check.
O(d) total, where d is the digit count — each digit is processed a small constant number of timesSpace: O(1)Common Mistakes
- Summing the digits only once and returning a multi-digit result.
- Using while (value > 10) so a two-digit multiple of a power of ten never reduces past 10.
- Forgetting that the digital root of 0 is 0.
Edge Cases to Test
- n = 0 returns 0 immediately.
- Single-digit inputs return themselves.
- Multiples of 9 (like 99 or 12345... check) always reduce to 9.
Interview Follow-Ups
- Can you prove why 1 + (n - 1) % 9 gives the digital root?
- How would you compute the digital root of a number too large to fit in a long?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →