Check Magic Number in Java
What you’ll learn
- Repeated digit sum approach for magic number checks.
- Single check and range listing in Java.
- How digital root connects to this interview pattern.
Prerequisites
- Basic Java syntax, loops, conditionals, and methods.
- Comfort with integer arithmetic and console input/output.
Math definition
This problem is driven by a deterministic numeric rule: for each valid input, apply the same formula repeatedly and classify the result.
Intuition with examples
Work a small input by hand first, then map each step to one Java loop iteration. This makes the control flow and stopping condition clear.
Live preview
Algorithm
Goal: check whether repeated digit summing ends at 1.
Reduce to one digit
While n > 9, set n to the sum of its digits using % 10 and / 10.
Final verdict
If the final single digit is 1, classify it as magic; otherwise not magic.
📜 Pseudocode
function isMagic(n):
while n > 9:
n = digitSum(n)
return n == 1Check single number (19)
public class Main {
static boolean isMagicNumber(int num) {
while (num > 9) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
return num == 1;
}
public static void main(String[] args) {
int number = 19;
if (isMagicNumber(number)) {
System.out.println(number + " is a Magic Number.");
} else {
System.out.println(number + " is not a Magic Number.");
}
}
}19 is a Magic Number.
Explanation
The outer loop reduces the number to one digit by repeated digit sums; if that digit is 1, the method returns true.
Magic numbers in range 1-50
public class Main {
static boolean isMagicNumber(int num) {
while (num > 9) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
return num == 1;
}
public static void main(String[] args) {
System.out.println("Magic Numbers in the range 1 to 50:");
for (int i = 1; i <= 50; i++) {
if (isMagicNumber(i)) {
System.out.print(i + " ");
}
}
System.out.println();
}
}Magic Numbers in the range 1 to 50: 1 10 19 28 37 46
Explanation
The same helper is reused for each number in the range and prints only values whose final reduced digit equals 1.
Optimization notes
Keep it simple. Prefer clear loops and method extraction before micro-optimizations.
Reuse computed values. Avoid recomputing the same sub-result inside nested loops.
❓ FAQ
🔄 Input / output examples
Use the sample programs as reference: provide valid numeric input and verify the output pattern matches the problem definition.
Input n | Result |
|---|---|
19 | Magic |
18 | Not magic |
1 | Magic |
28 | Magic |
Edge cases
Smallest valid input
Confirm behavior for minimum accepted values.
Invalid input
Guard against non-numeric or out-of-range values when input is user-provided.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Repeated digit-sum loops | O((log n)^2) conceptual | O(1) |
| Digital-root formula | O(1) | O(1) |
| Range scan | U single checks for [1, U] | O(1) |
Summary
- Use a direct Java implementation of the numeric rule.
- Validate input and test boundary cases.
- Discuss complexity clearly in interviews.
Repeatedly summing decimal digits until one digit remains is digital root logic. In this tutorial, a number is magic if that final digit is 1.
8 people found this page helpful
