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
n
Reduction
Magic?
1
(already 1)
Yes
10
10 → 1
Yes
19
19 → 10 → 1
Yes
18
18 → 9
No
Approach
How to Check a Magic Number in C#
Four clear steps — then pick a digit-sum loop or the O(1) formula.
Use a positive integer (interview convention).
While the value is greater than 9, replace it with the sum of its digits.
Stop when one digit remains.
Return true if that digit is 1 — or skip loops and test n % 9 == 1.
Method
Idea
Time
Best for
Digit-sum loop
Sum digits until n <= 9, check == 1
O(log n)
Interviews, showing the process
Modulo 9
n % 9 == 1 for n > 0
O(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
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Goal
Digital root equals 1
Stop when
n <= 9
Fast check
n % 9 == 1 (n > 0)
Not happy
Happy uses sum of squares
Same family
Condense / digital root
Classic check
19 yes; 18 no
Try it
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
Trace
Worked Walkthrough — n = 19
Sum digits, replace n, and repeat until one digit is left.
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");
}
}
}
Output
19 is a magic number
How It Works
Reject non-positive inputs for the usual interview definition.
Outer while (n > 9) means “keep going until one digit remains.”
Inner loop peels digits with n % 10 and builds sum.
Assign n = sum and repeat. For 19: 10, then 1.
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
}
}
Output
True
False
True
How It Works
Reject n <= 0 the same way as Example 1.
A number and the sum of its digits are congruent modulo 9.
Digital root 1 means n ≡ 1 (mod 9), i.e. n % 9 == 1.
That is why 19 % 9 == 1 (magic) and 18 % 9 == 0 (not magic — root 9).
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Digit-sum loop
O(log n)
O(1)
Modulo 9 shortcut
O(1)
O(1)
Each digit-sum pass costs O(digits). Few passes are needed because the sum shrinks quickly.
Remember
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).