PHP Condense a Number Program
Definition
What Does Condense a Number Mean?
To condense a number means repeatedly add its digits until only one digit remains. That final digit is the digital root.
while n has more than one digit:
n = sum of digits of n
Example: 9875
9 + 8 + 7 + 5 = 29
2 + 9 = 11
1 + 1 = 2 ← condensed result
| n | Reduction chain | Result |
|---|
38 | 38 → 11 → 2 | 2 |
999 | 999 → 27 → 9 | 9 |
7 | (already one digit) | 7 |
Approach
How to Condense a Number in PHP
Four clear steps — then pick an iterative loop or the O(1) formula.
- Work with a nonnegative value (use
abs() if needed). - While the value is greater than
9, replace it with the sum of its digits. - Stop when only one digit remains.
- Or skip the loops and compute the digital root with
n % 9.
| Method | Idea | Time | Best for |
|---|
| Digit-sum loop | Sum digits until n <= 9 | O(log n) | Interviews, showing the process |
| Modulo 9 formula | n == 0 ? 0 : 1 + (n - 1) % 9 | O(1) | Production code, quick answers |
Pseudocode
function Condense(n):
n = abs(n)
while n > 9:
sum = 0
while n > 0:
sum = sum + (n mod 10)
n = n / 10
n = sum
return n
Reference
Cheat Sheet
Keep these facts handy while writing the program.
| Topic | Detail |
|---|
| Goal | Reduce to a single digit 0–9 |
| Stop when | n <= 9 |
| One digit already | Return n unchanged |
| Fast formula | n == 0 ? 0 : 1 + (n - 1) % 9 |
| Multiples of 9 | For n > 0, digital root is 9 (not 0) |
| Classic check | 9875 → 2 |
Try it
Live Preview
Enter any integer to see the reduction chain and the condensed digit.
Trace
Worked Walkthrough — n = 9875
Sum digits, replace n, and repeat until one digit is left.
Step 1: 9 + 8 + 7 + 5 = 29
Step 2: 2 + 9 = 11
Step 3: 1 + 1 = 2
Stop (single digit)
Condensed value = 2
Check with formula:
1 + (9875 - 1) % 9 = 1 + 9874 % 9 = 1 + 1 = 2
Code
PHP Programs
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.
<?php
function condense(int $n): int
{
$n = abs($n);
while ($n > 9) {
$sum = 0;
while ($n > 0) {
$sum += $n % 10;
$n = intdiv($n, 10);
}
$n = $sum;
}
return $n;
}
$number = 9875;
echo $number . " condensed to " . condense($number) . "\n";
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.
<?php
function condenseFast(int $n): int
{
$n = abs($n);
if ($n === 0) {
return 0;
}
return 1 + ($n - 1) % 9;
}
echo condenseFast(9875) . "\n"; // 2
echo condenseFast(999) . "\n"; // 9
echo condenseFast(0) . "\n"; // 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–9Already condensed
Return the value as-is. No loop body should run.
Multiples of 9Root is 9
For n > 0, do not return 0 from a raw n % 9.
NegativesUse abs()
Digital root is usually defined for nonnegative integers.
One passKeep 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).
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful