Check Magic Number in C#

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
Repeated digit sum

What you’ll learn

  • What a magic number is: keep adding digits until one digit remains — it must be 1.
  • How to write IsMagicNumber in C# with nested digit-sum loops.
  • How to list magic numbers from 1 to 50 and trace any input with a live preview.

Overview

Magic numbers reuse the same digit-peeling skill as Harshad and Armstrong problems — but here you repeat the sum until the value shrinks to a single digit. If that digit is 1, the number is magic.

Two programs

Check 19 and scan 150 for all magic numbers.

Live preview

See each digit-sum step until the value becomes one digit.

Not happy numbers

Happy numbers square each digit; magic numbers only add digits.

Prerequisites

Digit extraction with % 10 and / 10, nested while loops.

  • using System;, static bool methods, Console.WriteLine.
  • Helpful: skim Harshad number for a single-pass digit sum.

What is a magic number?

A magic number is a positive integer that reduces to the single digit 1 when you repeatedly replace it with the sum of its digits.

For 19: 1 + 9 = 10, then 1 + 0 = 1 — so 19 is magic. For 20: 2 + 0 = 2 — not magic.

19 ends at 1
20 ends at 2
1 already 1

Repeated digit sum

Let S(n) be the sum of decimal digits of n. Apply S repeatedly until the result has one digit. That final value is the digital root. A magic number has digital root 1.

Trace for 19

19 → 10 → 1 (magic)

Quick examples

19 Magic
Steps
10, then 1
20 Not magic
Final
Single digit 2

Magic numbers 1–50: 1, 10, 19, 28, 37, 46.

Live preview

Enter a positive integer. The widget prints each digit-sum reduction until a single digit remains.

Try 19 (magic) or 20 (not magic).

Live result
Press “Trace steps” to run the digit-sum chain.

Algorithm

Goal: return whether positive n is magic.

While more than one digit

Loop while (num > 9) — any value 0–9 is already a single digit.

Sum the digits

Inner loop: add num % 10, divide num by 10. Set num = sum.

Check final digit

Return num == 1.

📜 Pseudocode

Pseudocode
function isMagic(n):  // n > 0
    while n > 9:
        sum ← 0
        while n > 0:
            sum ← sum + (n mod 10)
            n ← floor(n / 10)
        n ← sum
    return n = 1
1

Single check: is 19 magic?

Reference solution with outer and inner while loops. Uses class Program for consistency with other tutorials on this site.

c#
using System;

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

        while (num > 9)
        {
            int sum = 0;

            while (num > 0)
            {
                sum += num % 10;
                num /= 10;
            }

            num = sum;
        }

        return num == 1;
    }

    static void Main()
    {
        int number = 19;

        if (IsMagicNumber(number))
        {
            Console.WriteLine($"{number} is a Magic Number.");
        }
        else
        {
            Console.WriteLine($"{number} is not a Magic Number.");
        }
    }
}

Explanation

The outer loop keeps reducing 19 → 10 → 1. The inner loop computes each digit sum. When num is a single digit, we only check whether it equals 1.

while (num > 9)

Stop condition. Values 0–9 need no further digit summing.

2

Magic numbers from 1 to 50

Loops through the range and prints every magic number — same output as the reference tutorial.

c#
using System;

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

        while (num > 9)
        {
            int sum = 0;

            while (num > 0)
            {
                sum += num % 10;
                num /= 10;
            }

            num = sum;
        }

        return num == 1;
    }

    static void Main()
    {
        Console.WriteLine("Magic Numbers in the range 1 to 50:");

        for (int i = 1; i <= 50; i++)
        {
            if (IsMagicNumber(i))
            {
                Console.Write($"{i} ");
            }
        }

        Console.WriteLine();
    }
}

Explanation

Each value in the list differs by 9 from the previous — all share digital root 1. Numbers like 2 or 11 (root 2) are skipped.

Beyond the basics

SumOfDigits helper. Pull the inner loop into its own method — cleaner and reusable across Harshad, magic, and narcissistic-style problems.

Digital root shortcut. For positive n, if n % 9 == 1 then root is 1 (with n == 0 as special). Mention in interviews after writing the loop version.

Name clash: “magic number” also means hard-coded constants in compilers — this page is the digit-sum puzzle only.

❓ FAQ

A positive integer where you repeatedly replace the number with the sum of its digits until you reach a single digit — and that final digit is 1.
Yes. 19 → 1+9 = 10 → 1+0 = 1. The process ends at 1.
No. 20 → 2+0 = 2. The single-digit result is 2, not 1.
Yes. It is already the single-digit target value 1.
Happy numbers sum the squares of digits (19 → 82 → 68 → …). Magic numbers sum digits without squaring (19 → 10 → 1).
1, 10, 19, 28, 37, 46 — each has digital root 1.
For positive n, digital root 1 means n ≡ 1 (mod 9), with 9 mapping to root 9 not 1. The digit-sum loop is clearer in interviews.

🔄 Input / output examples

Change number in Main, or read from console with validation.

InputDigit-sum chainMagic?
11Yes
1010 → 1Yes
1919 → 10 → 1Yes
2020 → 2No
2828 → 10 → 1Yes
4646 → 10 → 1Yes

Edge cases and pitfalls

The logic is short, but distinguish magic numbers from similar-sounding problems.

One

1

The outer while (num > 9) never runs; num == 1 returns true immediately.

Zero

0

Not magic on this page — final digit would be 0, not 1. Reject non-positive input.

Happy

vs happy number

19 is both happy and magic, but the rules differ — do not mix sum-of-squares into magic checks.

Compiler

Other “magic” meaning

In systems programming, magic numbers are unexplained constants — unrelated to this digit puzzle.

⏱️ Time and space complexity

OperationTimeExtra space
Single checkO(log n) digits per pass, few passesO(1)
Range 1–50O(50 × log n)O(1)

Each digit-sum pass shrinks the number quickly; for 32-bit int values the loop runs only a handful of times.

Summary

  • Rule: repeat digit sum until one digit remains; magic iff that digit is 1.
  • C#: outer while (num > 9) + inner digit-sum loop, then num == 1.
  • Demo: 19 → 10 → 1; range 1–50 gives six magic values.
Did you know?

In the range 150, magic numbers are 1, 10, 19, 28, 37, 46 — every value is 9 apart after the first. That pattern appears because repeated digit sums eventually equal the digital root, and magic numbers are those whose digital root is 1.

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.

5 people found this page helpful