Condense a Number (Digital Root) in PHP

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Digit manipulation

What you’ll learn

  • How repeated digit sum gives a single-digit answer.
  • Iterative method and O(1) closed form with mod 9.
  • Important cases like 0, multiples of 9, and negatives.

Overview

Starting from a nonnegative integer, sum its decimal digits; if the result still has multiple digits, repeat. The final single digit is the digital root. Example: 9875 → 29 → 11 → 2.

Two programs

Iterative reduction and closed-form mod 9 method for nonnegative integers.

Live preview

Enter a number and instantly see its condensed value.

Terminology

Clear distinction between a one-time digit sum and the final digital root.

Prerequisites

Modulo, integer division, loops, and basic functions.

  • Use % 10 and intdiv($n, 10) to extract digits.
  • Optional: understand why mod 9 gives a shortcut in base 10.

Digital root vs digit sum

The digit sum of 9875 is 29. Condensing continues: 29 → 11 → 2.

Digit sum is one pass. Digital root repeats digit sum until one digit remains.

One passdigit sum
Repeatdigital root
mod 9closed form

Congruence mod 9

In base ten, n and the sum of its digits are congruent modulo 9. Repeating this preserves the same remainder, which yields the digital root shortcut.

9875

9875 % 9 = 2, matching the iterative chain ending at 2.

Intuition

18Root 9
Chain
1 + 8 = 9
49Root 4
Chain
4 + 9 = 13 → 1 + 3 = 4

Takeaway: repeated digit sums keep the same value modulo 9.

Live preview

Enter a nonnegative integer and get digital root.

Live result
Press "Condense" to see the digital root.

Algorithm (iterative)

Goal: reduce n to one digit by repeated digit sum.

While n > 9

Compute the sum of digits using % 10 and intdiv(n, 10).

Assign and repeat

Set n = digit_sum until n is a single digit.

📜 Pseudocode

Pseudocode
function condense(n):
    while n > 9:
        s = 0
        while n > 0:
            s += n % 10
            n = floor(n / 10)
        n = s
    return n
1

Iterative condensation

Reference-style loop: keep reducing until the working value is a single digit.

php
<?php
function condenseNumber(int $number): int
{
    $n = $number;
    while ($n > 9) {
        $digitSum = 0;
        while ($n > 0) {
            $digitSum += $n % 10;
            $n = intdiv($n, 10);
        }
        $n = $digitSum;
    }
    return $n;
}

$number = 9875;
echo "The condensed form of {$number} is: " . condenseNumber($number);
?>

Explanation

The inner loop accumulates digits into $digitSum; the outer loop repeats until the value is within 0..9.

2

Closed form using mod 9

Same answer for nonnegative inputs in O(1) time.

php
<?php
function digitalRootNonnegative(int $n): int
{
    if ($n === 0) return 0;
    $r = $n % 9;
    return $r === 0 ? 9 : $r;
}

echo "dr(9875) = " . digitalRootNonnegative(9875) . " (closed form)\n";
echo "dr(999999999) = " . digitalRootNonnegative(999999999) . " (closed form)\n";
?>

Explanation

Special-case 0. For positive values divisible by 9, return 9 instead of 0.

Optimization

Use formula. For nonnegative n, mod 9 gives the answer in O(1).

Huge inputs. For numbers beyond integer range, compute mod 9 over the number string.

Interview tip: explain iterative method first, then derive the shortcut and edge cases.

❓ FAQ

It means repeatedly adding digits until only one digit remains (digital root).
No. Digit sum is one pass. Condensing repeats until result is a single digit.
For n > 0: if n % 9 == 0 then 9 else n % 9. For n == 0, answer is 0.
In base 10, a number and sum of its digits are congruent modulo 9.
Usually we define this for nonnegative integers; you can use abs(n) if needed.
Iterative method is small digit-processing loops; closed form is O(1).

🔄 Input / output examples

Change the input literal in either sample and verify these expected roots.

InputDigital rootNotes
98752Reference walkthrough
00Special case
99Already one digit
1891 + 8 = 9

Edge cases and pitfalls

Define behavior for negatives up front; most implementations assume nonnegative integers.

Zero

n = 0

Digital root is 0.

Multiples of 9

n % 9 = 0 and n > 0

Root is 9, not 0.

Negative input

Define policy

Use abs(n) or reject input explicitly.

Huge values

Beyond integer range

Use string-based digit processing if values exceed numeric limits.

⏱️ Time and space complexity

ApproachTimeExtra space
Iterative digit reductionSmall digit loopsO(1)
Closed formO(1)O(1)

For string-based very large numbers, each pass is linear in the number of digits.

Summary

  • Condense means repeated digit sums until one digit remains.
  • Methods: iterative reduction and mod 9 shortcut for nonnegative inputs.
  • Important: handle 0, multiples of 9, and negative-input policy.
Did you know?

For positive numbers, digital root follows 1 + (n - 1) % 9.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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