C# Magic Number Program

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

What Is a Magic Number?

A magic number is a positive integer whose digits, when repeatedly summed, reduce to a single digit of 1 (digital root = 1).

Remember
while n has more than one digit:
    n = sum of digits of n

magic  ⇔  final digit == 1

Example: 19
  1 + 9 = 10
  1 + 0 = 1   ← magic

Example: 18
  1 + 8 = 9   ← not magic
nReductionMagic?
1(already 1)Yes
1010 → 1Yes
1919 → 10 → 1Yes
1818 → 9No

How to Check a Magic Number in C#

Four clear steps — then pick a digit-sum loop or the O(1) formula.

  1. Use a positive integer (interview convention).
  2. While the value is greater than 9, replace it with the sum of its digits.
  3. Stop when one digit remains.
  4. Return true if that digit is 1 — or skip loops and test n % 9 == 1.
MethodIdeaTimeBest for
Digit-sum loopSum digits until n <= 9, check == 1O(log n)Interviews, showing the process
Modulo 9n % 9 == 1 for n > 0O(1)Quick answers, production

Pseudocode

Pseudocode
function IsMagic(n):
    if n <= 0:
        return false

    while n > 9:
        sum = 0
        while n > 0:
            sum = sum + (n mod 10)
            n = n / 10
        n = sum

    return n == 1

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
GoalDigital root equals 1
Stop whenn <= 9
Fast checkn % 9 == 1 (n > 0)
Not happyHappy uses sum of squares
Same familyCondense / digital root
Classic check19 yes; 18 no

Live Preview

Enter a positive integer to see the reduction chain and whether it is magic.

Positive whole numbers. Tap a chip or type a value.

Result
n = 19
Chain: 19 → 10 → 1
Final digit = 1
Verdict: MAGIC NUMBER

Worked Walkthrough — n = 19

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

Calculation
Step 1: 1 + 9 = 10
Step 2: 1 + 0 = 1

Stop (single digit)
Final digit = 1 → magic

Shortcut check:
  19 % 9 = 1  ✓  (same answer)

C# Programs

Start with the iterative digit-sum loop so you can show each reduction. Then use the O(1) modulo shortcut when speed matters.

Example 1 — Digit-Sum Loop

Keep replacing the number with the sum of its digits until one digit remains, then check for 1.

C#
using System;

class Program
{
    static bool IsMagic(int n)
    {
        if (n <= 0)
        {
            return false;
        }

        while (n > 9)
        {
            int sum = 0;
            while (n > 0)
            {
                sum += n % 10;
                n /= 10;
            }
            n = sum;
        }

        return n == 1;
    }

    static void Main()
    {
        int number = 19;
        if (IsMagic(number))
        {
            Console.WriteLine(number + " is a magic number");
        }
        else
        {
            Console.WriteLine(number + " is not a magic number");
        }
    }
}

How It Works

  1. Reject non-positive inputs for the usual interview definition.
  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 19: 10, then 1.
  5. Return whether the final digit equals 1.

Example 2 — Modulo 9 Shortcut

Digital root is 1 exactly when n % 9 == 1 for positive n.

C#
using System;

class Program
{
    static bool IsMagic(int n)
    {
        if (n <= 0)
        {
            return false;
        }
        return n % 9 == 1;
    }

    static void Main()
    {
        Console.WriteLine(IsMagic(19)); // True
        Console.WriteLine(IsMagic(18)); // False
        Console.WriteLine(IsMagic(1));  // True
    }
}

How It Works

  1. Reject n <= 0 the same way as Example 1.
  2. A number and the sum of its digits are congruent modulo 9.
  3. Digital root 1 means n ≡ 1 (mod 9), i.e. n % 9 == 1.
  4. That is why 19 % 9 == 1 (magic) and 18 % 9 == 0 (not magic — root 9).
  5. Same answers as the loop, but in constant time.

Edge Cases & Pitfalls

Check these before calling the solution done.

1

Already magic

Single digit 1 is magic; other single digits are not.

Multiples of 9

Root is 9

18, 27, … reduce to 9, not 1 — never magic.

Happy vs magic

Different process

Happy squares each digit; magic only adds them.

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 shortcutO(1)O(1)

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

Key Takeaways

  • Definition: digital root equals 1.
  • Loop: peel digits with % 10 while n > 9.
  • Shortcut: n % 9 == 1 for n > 0.
  • Test set: 19 yes; 10 yes; 1 yes; 18 no.

One line: repeatedly sum digits until one digit — magic if that digit is 1.

Frequently Asked Questions

Repeatedly sum the decimal digits until one digit remains. If that digit is 1, the number is magic.
Yes. It is already a single digit and equals 1.
No. Happy numbers use the sum of squared digits. Magic numbers here use plain digit sums.
The final one-digit value is the digital root. Magic means digital root equals 1.
Yes. For n > 0, magic iff n % 9 == 1 (same as digital root 1).

Next: Matrix Addition

Add two matrices element by element in C#.

Matrix addition 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