easyDigit ProblemsJava

Find the Digital Root of a Number

Reduce a number to a single digit by repeatedly summing its digits.

Quick Answer

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

Example 1
Input:  9875
Output: 2

9+8+7+5 = 29, then 2+9 = 11, then 1+1 = 2.

Example 2
Input:  12345
Output: 6

1+2+3+4+5 = 15, then 1+5 = 6.

Constraints

  • 0 <= n <= 2,147,483,647
  • No 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
First write the plain digit-sum loop; the digital root just applies it over and over.
Hint 2
Wrap the digit-sum in an outer loop that keeps running while the current value is 10 or more.
Hint 3
For a shortcut, note that digital roots cycle through 1..9: for n > 0 the answer is 1 + (n - 1) % 9, and 0 stays 0.

Approach

Reveal the step-by-step approach

Reduce the number until only one digit is left.

  1. While value >= 10:
    • Compute the digit sum of value with an inner % 10 / / 10 loop.
    • Replace value with that digit sum.
  2. When value drops 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.

Time: 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 →
Chat with us