Definition
Final digit 1
Repeated digit sum until one digit; magic iff that digit is 1.
Magic numbers (in this tutorial) collapse by repeated decimal digit sums until one digit remains — and that digit must be 1. This page covers the definition, digital-root intuition, a live preview, algorithm steps, worked C examples, edge cases, and complexity.
Final digit 1
Repeated digit sum until one digit; magic iff that digit is 1.
% 10 loop
Peel digits with n % 10, accumulate, divide by 10.
Same idea
Magic means digital root equals 1 in base 10.
1–50
List every magic value in a closed interval with the same helper.
Step trace
Watch each digit-sum step before you compile C.
No squares
Happy numbers square digits; magic numbers only sum them.
A magic number (for this tutorial) is a positive integer that reduces to 1 when you repeatedly replace it by the sum of its decimal digits. For 19: 1 + 9 = 10, then 1 + 0 = 1 — so 19 is magic.
In C interviews you usually write nested loops (outer until one digit, inner digit sum), then optionally list magic numbers in a range — and clarify that this is not the same as happy numbers.
It trains digit extraction, nested loops, and digital-root intuition — skills that reappear in happy numbers, Harshad checks, and checksum-style problems.
Only the last single digit decides magic.
Outer until n ≤ 9; inner sums digits.
Classic whiteboard trace for interviews.
No squared digits — plain digit sum only.
In short: keep summing decimal digits until one digit remains; the number is magic iff that digit is 1.
Given a positive integer n, decide whether it is magic under the repeated digit-sum rule; optionally list every magic value in a closed interval.
/* n = 19
* 19 → 1+9 = 10
* 10 → 1+0 = 1 → magic
*
* n = 18
* 18 → 1+8 = 9 → not magic
*/ | Item | Type | Description |
|---|---|---|
number / n | int | Positive integer to classify (Example 1 uses 19). |
| Range bounds | int | Inclusive interval such as [1, 50] (Example 2). |
| Result | flag / text | Magic or not; or a printed list of magic values. |
function digit_sum(n): // n >= 0
s ← 0
while n > 0:
s ← s + (n mod 10)
n ← floor(n / 10)
return s
function is_magic(n): // assume n > 0
while n > 9:
n ← digit_sum(n)
return (n = 1) | Method | Idea | Extra space |
|---|---|---|
| Nested loops | Outer until one digit; inner digit sum | O(1) |
| Digital-root formula | 1 + (n - 1) % 9 equals 1 | O(1) |
| Goal | Pattern |
|---|---|
| Next digit | sum += num % 10; num /= 10; |
| Outer reduce | while (num > 9) { ... num = sum; } |
| Magic test | return num == 1; |
| O(1) shortcut | 1 + (n - 1) % 9 == 1 for n > 0 |
| Classic probes | Test 19, 18, and 1 |
Related digit problems — only magic uses plain digit sum to digital root 1.
sum → 1Repeated digit sum ends at 1
sq sumSum of squared digits until cycle / 1
n % s(n)One digit sum, then divisibility
loop firstMention digital-root formula only if asked
Reach for magic-number drills when digit sums and digital roots matter.
Nested loops, digit extraction, and a clear stop condition.
Pairs with happy, Harshad, and other digit walks.
Digit sums appear in simple validation schemes.
List or count magic numbers in [L, R].
Unrelated to unexplained numeric literals in source code.
Key benefit: a tiny nested-loop problem that builds the exact muscle memory you need for other digit-property interviews.
Enter a positive integer to watch the same digit-sum chain as the C code.
Two complete C programs — classify a single value, and list magic numbers in [1, 50]. Click View Output to reveal sample console results.
Nested digit-sum loops for n = 19.
19Outer loop continues until one digit; inner loop computes one digit-sum pass.
#include <stdio.h>
/* Returns 1 if num is a magic number (repeated digit sum ends at 1), else 0 */
int is_magic_number(int num) {
while (num > 9) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
return num == 1;
}
int main(void) {
int number = 19;
if (is_magic_number(number)) {
printf("%d is a Magic Number.\n", number);
} else {
printf("%d is not a Magic Number.\n", number);
}
return 0;
} The outer while (num > 9) keeps collapsing the value; the inner while is one pass of digit summation. When num is a single digit, compare it to 1.
Reuse the same helper across a closed interval.
[1, 50]Scan each i independently; listing matches the classic reference output.
#include <stdio.h>
int is_magic_number(int num) {
while (num > 9) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
return num == 1;
}
int main(void) {
printf("Magic Numbers in the range 1 to 50:\n");
for (int i = 1; i <= 50; ++i) {
if (is_magic_number(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
} Reuse is_magic_number for each i. Values congruent to 1 mod 9 (with care at multiples of 9) tend to land here — adjust 50 for other intervals.
Assume a positive integer (samples use n > 0).
While n > 9, replace n by the sum of its decimal digits.
When one digit remains, return whether it equals 1.
For 19, the chain is 19 → 10 → 1 — magic.
n = 19Trace each digit-sum pass until a single digit remains.
| Step | Current n | Digit sum | Note |
|---|---|---|---|
1 | 19 | 1 + 9 = 10 | still two digits |
2 | 10 | 1 + 0 = 1 | single digit |
3 | 1 | — | 1 == 1 → magic |
By contrast, 18 → 9 stops at 9, so it is not magic.
Where magic-number thinking shows up beyond the interview prompt.
Build fluency with % 10 / /= 10 extraction.
Example: same helper reused for digital-root problems.
Outer stop condition plus an inner reduction pass.
Example: while (num > 9) wrapping a digit loop.
Connect the loop to the closed-form digital root.
Example: 1 + (n - 1) % 9.
List or count magic numbers in an interval.
Example: all magic in 1–50.
Clarify sum vs sum-of-squares when both appear on a sheet.
Example: previous happy-number page.
Harshad and Disarium share digit-walk muscle memory.
Example: Harshad uses one sum, not iteration to 1.
Pro Tip: if the interviewer mentions digital roots, say magic means digital root equals 1 — then offer the loop as your primary solution.
Why this approach earns interview points.
Whiteboard steps match the nested-loop structure one-for-one.
Digit counts shrink quickly; cost is effectively constant in practice.
One is_magic_number powers single checks and range scans.
You can mention the digital-root formula after the loop version.
Pro Tip: implement the loop first; offer 1 + (n - 1) % 9 only as a speed follow-up.
Small habits that keep magic-number code clean in interviews.
State n > 0 at the API boundary before digit work.
Use while (num > 9), not an arbitrary iteration count.
Say out loud that you are not squaring digits.
Magic, not magic, and the single-digit edge case.
Keep the loop as the primary answer unless asked for O(1).
Pro Tip: dry-run 19 on paper (table above) before coding — it locks in both nested loops.
Mistakes that commonly break magic-number solutions in C.
Happy-number muscle memory can sneak into magic checks.
→ Sum digits only — never square them here.
19 → 10 is not finished; you must continue until one digit.
→ Keep the outer while (num > 9) loop.
Digit sum of 0 stays 0, which is not 1.
→ Reject or document n <= 0 as non-magic.
Digit loops on negatives need an explicit rule.
→ Reject negatives, or take absolute value and document it.
Unexplained literals in code are a different meaning of the phrase.
→ Clarify the digit-sum definition when starting your answer.
Check these inputs before calling the solution done.
Already 1 — magic with zero reduction passes.
n = 0Not classified as magic; digit sum stays 0.
n = 1818 → 9, final digit is not 1.
n = 2828 → 10 → 1 — magic.
Reject or document absolute-value handling.
Compiler “magic numbers” are unrelated literals in source.
Known results for common interview inputs.
Test number | Typical line printed |
|---|---|
19 | 19 is a Magic Number. |
18 | 18 is not a Magic Number. |
1 | 1 is a Magic Number. |
28 | 28 is a Magic Number. |
Try these variations to lock in the pattern.
scanf and validate n > 0[1, 100]?is_magic_number1 + (n - 1) % 9 == 11.Quick Takeaway: keep summing digits until one digit remains; magic iff that digit is 1.
| Approach | Time (single n) | Extra space |
|---|---|---|
| Repeated digit-sum loops | O((log n)²) digit ops for typical int | O(1) |
| Digital-root formula | O(1) arithmetic | O(1) |
Range [1, U] | U times the single-check cost | O(1) |
Magic numbers are a small digit-sum exercise with clear interview payoff: nested loops, digital-root intuition, and a sharp contrast with happy numbers. Master the single-check helper and the range scan so you can adapt either stop condition on the spot.
Practice the two examples above, then continue to matrix addition for a 2D array warm-up.
Keep summing digits until one digit remains; magic iff that digit is 1.
Classify them the interview-friendly way.
Digital root = 1
DefinitionOuter + digit sum
CodeNo squares
ContrastOptional O(1)
Follow-upO((log n)²)
AnalysisRepeatedly summing decimal digits until you reach a single digit is the same idea as the digital root in base 10. For this page, a magic number is one whose digital root is 1 (for example 19 → 1+9=10 → 1+0=1).
Learn how to add two matrices element-wise with 2D arrays in C.
8 people found this page helpful