Calculate Digital Root in Java
Problem
A number's digital root is the single digit left after repeatedly replacing it with the sum of its own digits, until only one digit remains.
Given a number, find its digital root.
Java Program
public class DigitalRootCalculator {
// Sums the digits of a single number in one pass
static int digitSum(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
public static void main(String[] args) {
int n = 12345;
while (n >= 10) {
n = digitSum(n); // one full digit-summing pass per iteration
}
System.out.println("Digital root: " + n);
}
}Output
Core Logic
A small helper that sums one number's digits can be called again and again on its own output, shrinking the value one round at a time until only a single digit survives.
- 1
digitSum(n)is a standalone helper — it peels digits off with%10//10and returns their total, with no knowledge of how many times it'll be called. - 2The main loop keeps replacing
nwithdigitSum(n)as long asnstill has two or more digits. - 3Each call to the helper is a complete, independent digit-summing pass — the loop just decides whether another pass is needed.
- 4Once
digitSum(n)would return a value under 10, the loop stops andnalready holds the digital root.
12345, the first call to digitSum() returns 15; since that's still two digits, a second call on 15 returns 6, and the loop stops there.Key Point: Pulling the digit-summing logic into its own method means the loop reads as 'keep summing until one digit remains' rather than a nested loop doing two jobs at once.
Why: Each pass strips off roughly as many digits as the previous pass's result had, so the total digit-visits across every pass stay proportional to n's original digit count.
Key Concepts
Approach 2: Using the Digital Root Formula
public class DigitalRootFormula {
public static void main(String[] args) {
int n = 12345;
// The digital root formula, with n == 0 handled as a special case
int digitalRoot = n == 0 ? 0 : 1 + (n - 1) % 9;
System.out.println("Digital root: " + digitalRoot);
}
}
Output
Core Logic
Every digit-summing pass leaves a number's remainder mod 9 unchanged, and that single invariant is enough to skip straight to the final answer.
- 1Because digit-summing never changes a number's value mod 9, the digital root itself must equal
n mod 9— adjusted so the result lands between 1 and 9 instead of 0 and 8. - 2
1 + (n - 1) % 9applies that adjustment in one expression, with no digits ever inspected. - 3
n == 0is carved out as its own case first, since it's the one input where the digital root is genuinely0, not 1 through 9.
n = 12345: 1 + (12345 - 1) % 9 = 1 + 12344 % 9 = 1 + 5 = 6, matching the two passes the loop took to get there.Key Point: No matter how many passes the loop-based version would need for a huge input, this formula answers in one step, since it's built from the mod-9 invariant rather than simulating the passes themselves.
Why: The digital root reduces to a single arithmetic expression regardless of how many digits n has.