Magic Number in Java with Program & Explanation
Welcome, Java learners! 🎯 In this post, you'll learn how to write a Magic Number program in Java — one of the most popular coding interview questions for beginners and freshers.
We'll cover the definition, the algorithm, a complete working Java program with line-by-line explanation, and the expected output.
What is a Magic Number?
A Magic Number is a number whose digits are repeatedly summed until a single digit is obtained. If that single digit equals 1, the original number is called a Magic Number.
Example — is 199 a Magic Number?
- 199 → 1 + 9 + 9 = 19
- 19 → 1 + 9 = 10
- 10 → 1 + 0 = 1 ✅
Since the final single digit is 1, 199 is a Magic Number.
Other Magic Numbers: 1, 10, 19, 28, 37, 46, 55, 64, 73, 82, 91, 100…
Algorithm to Find Magic Number
- Read the number
num. - Store a copy in
ttfor printing later. - While
s > 9:- Set
num = sands = 0. - While
num > 0:- Extract last digit:
d = num % 10. - Add to sum:
s = s + d. - Remove last digit:
num = num / 10.
- Extract last digit:
- Set
- If
s == 1→ print "Magic Number". - Else → print "Not a Magic Number".
Complete Java Program
Here is the complete, runnable Java program:
public class First {
/*
* A number is said to be a Magic number if the sum of its digits is
* calculated till a single digit is obtained by recursively adding the sum
* of its digits. If the single digit comes to be 1 then the number is a
* magic number.
*
* Example: 199 is a magic number as 1+9+9=19 but 19 is not a single digit
* number so 1+9=10 and then 1+0=1 which is a single digit number and also 1.
* Hence it is a magic number.
*/
void magicNumber(int num) {
int d, s;
int tt = num;
s = num;
// Repeat until a single digit is obtained
while (s > 9) {
num = s;
s = 0;
// Sum the digits of num
while (num > 0) {
d = num % 10;
s = s + d;
num = num / 10;
}
}
if (s == 1)
System.out.println("magic number :- " + tt);
else
System.out.println("not a magic number :- " + tt);
}
public static void main(String[] args) {
new First().magicNumber(55);
}
}
Line-by-Line Explanation
-
void magicNumber(int num)— the method that checks if a number is magic. -
int tt = num;— stores the original number for the final print. -
s = num;— initializes the sum with the original number. -
while (s > 9)— outer loop runs while the sum is still a multi-digit number. -
num = s; s = 0;— resetsnumto the current sum, and resetssfor the inner loop. -
while (num > 0)— inner loop iterates through all digits. -
d = num % 10;— extracts the last digit. -
s = s + d;— adds digit to the sum. -
num = num / 10;— removes the last digit. -
if (s == 1)— checks if the final single digit is 1.
Output
For input 55:
55 → 5 + 5 = 10 → 1 + 0 = 1 ✅
magic number :- 55
Try these test cases:
199→ 1+9+9 = 19 → 1+9 = 10 → 1+0 = 1 → Magic Number ✅100→ 1+0+0 = 1 → Magic Number ✅123→ 1+2+3 = 6 → Not a Magic Number ❌999→ 9+9+9 = 27 → 2+7 = 9 → Not a Magic Number ❌
Frequently Asked Questions
What is a Magic Number in Java?
A Magic Number is a number whose digits are repeatedly summed until a single digit is obtained. If that single digit equals 1, the original number is called a Magic Number.
How do you find a Magic Number in Java?
Use nested loops: the outer loop keeps the number greater than 9, and the inner loop
sums the digits using modulo (%) and integer division (/).
After the loop ends, check if the final single digit is 1.
Is 55 a Magic Number?
Yes. 5 + 5 = 10 → 1 + 0 = 1, so 55 is a Magic Number.
Which numbers are Magic Numbers?
Magic Numbers are those whose recursive digit sum equals 1. Examples: 1, 10, 19, 28, 37, 46, 55, 64, 73, 82, 91, 100, 109, 118…
Is Magic Number the same as Happy Number?
No. A Happy Number involves squaring digits repeatedly until you reach 1. A Magic Number uses simple digit summation. They are different concepts.
Conclusion
In this post, you learned how to write a Magic Number program in Java with complete source code and line-by-line explanation. The key idea is to keep summing digits until a single digit remains, then check if it equals 1.
This is a classic interview question — practice it until you can write it without looking at the solution. Once you're comfortable, try variations like:
- Finding all Magic Numbers between 1 and 100
- Finding the nth Magic Number
- Magic Number using recursion
No comments:
Post a Comment