C# Condense a Number Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

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.

Remember
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
nReduction chainResult
3838 → 11 → 22
999999 → 27 → 99
7(already one digit)7

How to Condense a Number in C#

Four clear steps — then pick an iterative loop or the O(1) formula.

  1. Work with a nonnegative value (use Math.Abs if needed).
  2. While the value is greater than 9, replace it with the sum of its digits.
  3. Stop when only one digit remains.
  4. Or skip the loops and compute the digital root with n % 9.
MethodIdeaTimeBest for
Digit-sum loopSum digits until n <= 9O(log n)Interviews, showing the process
Modulo 9 formulan == 0 ? 0 : 1 + (n - 1) % 9O(1)Production code, quick answers

Pseudocode

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

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
GoalReduce to a single digit 0–9
Stop whenn <= 9
One digit alreadyReturn n unchanged
Fast formulan == 0 ? 0 : 1 + (n - 1) % 9
Multiples of 9For n > 0, digital root is 9 (not 0)
Classic check9875 → 2

Live Preview

Enter any integer to see the reduction chain and the condensed digit.

Whole numbers (absolute value used). Tap a chip or type a value.

Result
n = 9875
Chain: 9875 → 29 → 11 → 2
Condensed = 2

Worked Walkthrough — n = 9875

Sum digits, replace n, and repeat until one digit is left.

Calculation
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

C# 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.

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));
    }
}

How It Works

  1. Take the absolute value so negatives behave like positives.
  2. Outer while (n > 9) means “keep going until one digit remains.”
  3. Inner loop peels digits with n % 10 and builds sum.
  4. Assign n = sum and repeat. For 9875: 29, then 11, then 2.
  5. 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
    }
}

How It Works

  1. 0 is a special case — its digital root is 0.
  2. For any positive n, (n - 1) % 9 maps multiples of 9 to the right slot.
  3. Adding 1 yields digits 1–9. That is why 999 becomes 9, not 0.
  4. Same answers as the loop, but in constant time with no digit scanning.
  5. 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.

Time and Space Complexity

ProgramTimeExtra space
Digit-sum loopO(log n)O(1)
Modulo 9 formulaO(1)O(1)

Each digit-sum pass costs O(digits). Few passes are needed because the sum shrinks quickly.

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).

Next: Cube Number

Check whether a number is a perfect cube.

Cube number tutorial →

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