- Chain
1+8 = 9
Condense a Number (Digital Root) in Java
What you’ll learn
- How repeated digit sums produce the digital root (the usual meaning of “condense” here).
- An iterative Java program matching the reference flow, plus an O(1) base-10 formula for nonnegative inputs.
- A live preview, modular arithmetic intuition (
mod 9), and edge cases such as zero and large magnitudes.
Overview
Starting from a nonnegative integer, sum its decimal digits; if the result still has more than one digit, repeat. The final single digit is the digital root. Example: 9875 → 29 → 11 → 2.
Two programs
Iterative peel (reference style) and a closed form using % 9.
Live preview
Type a nonnegative integer and see the condensed digit.
Terminology
Clarifies digital root vs a single digit sum pass.
Prerequisites
Decimal representation, % 10 and / 10 digit peeling, and nested loops.
public static void main(String[] args), integer arithmetic, and printing withSystem.out.println.- Optional: modular arithmetic basics (
n % 9) to understand the closed form.
Digital root vs digit sum
The digit sum of 9875 is 9+8+7+5 = 29. That is not yet one digit. The digital root keeps summing digits until the value lies in 0..9; here 29 → 11 → 2.
Many tutorials call this process “condensing” the number. It is not a different base; it is repeated reduction in base ten.
Congruence mod 9
Because 10 ≡ 1 (mod 9), every decimal digit expansion satisfies n ≡ s(n) (mod 9) where s(n) is the digit sum. The same congruence holds after each condensation step, so the digital root is determined by n mod 9 with the usual 9 substitution for nonzero multiples of 9.
98759875 mod 9 = 2, matching the iterated sum chain ending at 2.
Intuition
- Chain
13 → 4
Takeaway: each digit-sum step preserves the value modulo 9 (except the bookkeeping that maps a remainder of 0 to digital root 9 for positive n).
Live preview
Nonnegative integers in the JavaScript safe range. Uses the same repeated digit-sum rule as Example 1.
Algorithm (iterative)
Goal: reduce a nonnegative integer to a single decimal digit by repeated digit summation.
While the working value exceeds 9
Replace it by the sum of its decimal digits (peel with % 10 and / 10).
Stop
Return the last value (already in 0..9).
Closed form (base 10, n ≥ 0)
If n == 0, result 0. Else let r = n % 9; result is 9 if r == 0, else r.
📜 Pseudocode
function condense(n): // n ≥ 0
while n > 9:
sum ← 0
t ← n
while t > 0:
sum ← sum + (t mod 10)
t ← floor(t / 10)
n ← sum
return nIterative condensation (reference flow)
Uses a working copy n so the outer loop condition is clear. Assumes nonnegative number; main keeps the original for printing.
public class Main {
static int condenseNumber(int number) {
int n = number;
while (n > 9) {
int sum = 0;
int tmp = n;
while (tmp > 0) {
sum += tmp % 10;
tmp /= 10;
}
n = sum;
}
return n;
}
public static void main(String[] args) {
int number = 9875;
int condensedNumber = condenseNumber(number);
System.out.println("The condensed form of " + number + " is: " + condensedNumber);
}
}Explanation
The inner loop destroys tmp on each pass; n holds the next value to condense. The parameter number in main is unchanged because Java passes primitives by value.
while (n > 9)Stopping rule. Single-digit results (including 0) exit immediately.
Closed form with long (mod 9)
Same mathematical answer for nonnegative inputs in a single step. Useful when n fits in long and you want O(1) time.
public class Main {
// Precondition: n >= 0
static int digitalRootNonnegative(long n) {
if (n == 0) {
return 0;
}
int r = (int)(n % 9);
return (r == 0) ? 9 : r;
}
public static void main(String[] args) {
long a = 9875L;
long b = 999999999999999999L; // 18 nines
System.out.println("dr(" + a + ") = " + digitalRootNonnegative(a) + " (closed form)");
System.out.println("dr(" + b + ") = " + digitalRootNonnegative(b) + " (closed form)");
}
}Explanation
Eighteen nines sum to 162, whose digit sum is 9; the shortcut returns 9 immediately because n % 9 == 0 for nonzero n.
return (r == 0) ? 9 : r;Map remainder 0 to root 9 for positive multiples of nine.
Optimization
mod 9 trick. Use the closed form when n is wide but fits your integer type; it avoids repeated scans of long digit strings.
Big integers. If n is stored as a string, either iterate digits once per round or reduce mod 9 in one pass over characters without building full long values.
Interview: define nonnegative policy, give 9875 trace, then cite the mod 9 formula.
❓ FAQ
🔄 Input / output examples
Swap the literal in main or read with Scanner for interactive tests in Java.
| Input | Digital root | Notes |
|---|---|---|
9875 | 2 | Reference walkthrough |
0 | 0 | Both programs |
9 | 9 | Already one digit |
18 | 9 | 1+8 |
Edge cases and pitfalls
The reference loop while (n > 9) never terminates for some negative conventions if you feed negatives in—keep inputs nonnegative or normalize first.
n = 0
The outer while is skipped; digital root 0. The closed form must special-case n == 0 before the 9 substitution.
n % 9 == 0, n > 0
The root is 9, not 0. Mixing that up breaks the closed form.
Wide literals
Iterative summation on huge values needs wide intermediates; long plus % 9 is simpler when exact n fits.
Beyond decimal
The mod 9 shortcut is specific to base ten. Other bases use different moduli.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
Iterative (32-bit int) | Small constant number of digit passes | O(1) |
| Iterative (asymptotic) | O(D · R) digit work (digits D, rounds R; both tiny for interview int) | O(1) |
| Closed form | O(1) | O(1) |
For very large n as a string, one digit pass per round is O(L) per round in string length L.
Summary
- Idea: repeated digit sum until one digit—the digital root.
- Code: nested loops, or
n==0 ? 0 : (n%9==0?9:n%9)for nonnegativen. - Watch-outs: clarify vs one-shot digit sum,
0, multiples of9, negatives.
In base ten, the digital root (this page’s “condensed” value) is related to divisibility by 9: for n > 0, repeated digit sums eventually match 1 + (n - 1) % 9, with 9 instead of 0 when n is a nonzero multiple of 9.
8 people found this page helpful
