Start with the iterative digit-sum loop so you can show each reduction. Then use the O(1) modulo formula when speed matters.
Example 1 — Digit-Sum Loop
Keep replacing the number with the sum of its digits until it is a single digit.
C#
using System;
class Program
{
static int Condense(int n)
{
n = Math.Abs(n);
while (n > 9)
{
int sum = 0;
while (n > 0)
{
sum += n % 10;
n /= 10;
}
n = sum;
}
return n;
}
static void Main()
{
int number = 9875;
Console.WriteLine(number + " condensed to " + Condense(number));
}
}
Output
9875 condensed to 2
How It Works
Take the absolute value so negatives behave like positives.
Outer while (n > 9) means “keep going until one digit remains.”
Inner loop peels digits with n % 10 and builds sum.
Assign n = sum and repeat. For 9875: 29, then 11, then 2.
Return the final single digit.
Example 2 — Modulo 9 Formula
Use the base-10 identity: the digital root equals 1 + (n - 1) % 9 for n > 0.
C#
using System;
class Program
{
static int CondenseFast(int n)
{
n = Math.Abs(n);
if (n == 0)
{
return 0;
}
return 1 + (n - 1) % 9;
}
static void Main()
{
Console.WriteLine(CondenseFast(9875)); // 2
Console.WriteLine(CondenseFast(999)); // 9
Console.WriteLine(CondenseFast(0)); // 0
}
}
Output
2
9
0
How It Works
0 is a special case — its digital root is 0.
For any positive n, (n - 1) % 9 maps multiples of 9 to the right slot.
Adding 1 yields digits 1–9. That is why 999 becomes 9, not 0.
Same answers as the loop, but in constant time with no digit scanning.
Use the loop in interviews when you must show the reduction; use this formula in real code.
Edge Cases & Pitfalls
Check these before calling the solution done.
0–9
Already condensed
Return the value as-is. No loop body should run.
Multiples of 9
Root is 9
For n > 0, do not return 0 from a raw n % 9.
Negatives
Use Math.Abs
Digital root is usually defined for nonnegative integers.
One pass
Keep repeating
A single digit sum is not enough when the sum itself has two digits.
Analysis
Time and Space Complexity
Program
Time
Extra space
Digit-sum loop
O(log n)
O(1)
Modulo 9 formula
O(1)
O(1)
Each digit-sum pass costs O(digits). Few passes are needed because the sum shrinks quickly.
Remember
Key Takeaways
Definition: keep summing digits until one digit remains (digital root).
Loop: peel digits with % 10 while n > 9.
Formula:1 + (n - 1) % 9 for n > 0.
Test set: 9875 → 2; 999 → 9; 7 → 7; 0 → 0.
One line: repeatedly sum digits — or jump straight to the digital root with modulo 9.
Frequently Asked Questions
It means repeatedly adding the digits of a number until only one digit remains. That final digit is called the digital root.
A digit sum is one pass. Condensing keeps summing until the result is a single digit from 0 to 9.
For n > 0: if n % 9 == 0 then 9, else n % 9. For n == 0 the answer is 0. Equivalently: 1 + (n - 1) % 9 for n > 0.
In base 10, a number and the sum of its digits are congruent modulo 9, so repeated digit sums preserve that congruence.
The iterative method is O(log n) per reduction step and needs only a few steps. The closed-form mod-9 method is O(1).