- Steps
- 10, then 1
Check Magic Number in C#
What you’ll learn
- What a magic number is: keep adding digits until one digit remains — it must be
1. - How to write
IsMagicNumberin 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 1–50 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 boolmethods,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.
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.
1919 → 10 → 1 (magic)
Quick examples
- 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.
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
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 Single check: is 19 magic?
Reference solution with outer and inner while loops. Uses class Program for consistency with other tutorials on this site.
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.
Magic numbers from 1 to 50
Loops through the range and prints every magic number — same output as the reference tutorial.
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
🔄 Input / output examples
Change number in Main, or read from console with validation.
| Input | Digit-sum chain | Magic? |
|---|---|---|
1 | 1 | Yes |
10 | 10 → 1 | Yes |
19 | 19 → 10 → 1 | Yes |
20 | 20 → 2 | No |
28 | 28 → 10 → 1 | Yes |
46 | 46 → 10 → 1 | Yes |
Edge cases and pitfalls
The logic is short, but distinguish magic numbers from similar-sounding problems.
1
The outer while (num > 9) never runs; num == 1 returns true immediately.
0
Not magic on this page — final digit would be 0, not 1. Reject non-positive input.
vs happy number
19 is both happy and magic, but the rules differ — do not mix sum-of-squares into magic checks.
Other “magic” meaning
In systems programming, magic numbers are unexplained constants — unrelated to this digit puzzle.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
| Single check | O(log n) digits per pass, few passes | O(1) |
| Range 1–50 | O(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, thennum == 1. - Demo:
19 → 10 → 1; range 1–50 gives six magic values.
In the range 1–50, 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.
5 people found this page helpful
