Java ProgramsControl FlowCalculate Digital Root

Calculate Digital Root in Java

intermediate·  Control Flow  ·  Loops

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.

Input
12345
Output
Digital root: 6

Java Program

Java
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

Digital root: 6

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.

How It Works
  1. 1digitSum(n) is a standalone helper — it peels digits off with %10//10 and returns their total, with no knowledge of how many times it'll be called.
  2. 2The main loop keeps replacing n with digitSum(n) as long as n still has two or more digits.
  3. 3Each call to the helper is a complete, independent digit-summing pass — the loop just decides whether another pass is needed.
  4. 4Once digitSum(n) would return a value under 10, the loop stops and n already holds the digital root.
For 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.

Complexity
Time Complexity: O(log n)Space Complexity: O(1)

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

while loopdigit sumrepeated reduction

Approach 2: Using the Digital Root Formula

Java
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

Digital root: 6

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.

How It Works
  1. 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. 21 + (n - 1) % 9 applies that adjustment in one expression, with no digits ever inspected.
  3. 3n == 0 is carved out as its own case first, since it's the one input where the digital root is genuinely 0, not 1 through 9.
For 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.

Complexity
Time Complexity: O(1)Space Complexity: O(1)

Why: The digital root reduces to a single arithmetic expression regardless of how many digits n has.

Key Concepts

modulo arithmeticclosed-form formula

Related Programs