In this article, we will explore how to write a Magic Number Program in Java to determine whether a given number is magical.
A number is considered a Magic Number if the recursive sum of its digits reduces to a single digit, and that single digit is 1. For example, 19 is a magic number because:
- Add its digits: 1+9=101 + 9 = 101+9=10
- Add the digits of the result: 1+0=11 + 0 = 11+0=1.
Since the final output of sum is 1, it will be called a magic number.
What is a Magic Number?
A number will be considered a Magic Number, if you keep adding its digits together until you get a single digit. If that final single digit is 1, then the number is a magic number.
Steps to Check for a Magic Number in Java
- Input the number: Accept the number from the user.
- Calculate the sum of digits: Use a loop or recursion to sum the digits.
- Repeat the process: Continue summing the digits until you get a single digit.
- Check if the result is 1: If yes, it’s a magic number.
Magic number program in java
Let’s follow the steps mentioned above into a Java program, and check whether the given number is magic or not.
import java.util.Scanner;
public class MagicNumber {
// This method calculates the sum of digits
static int sumOfDigits(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10; // Add the last digit
num /= 10; // Remove the last digit
}
return sum;
}
// This method checks if the number is a magic number
static boolean isMagicNumber(int num) {
while (num > 9) {
num = sumOfDigits(num); // Reduce to a single digit
}
return num == 1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (isMagicNumber(number)) {
System.out.println(number + " is a Magic Number!");
} else {
System.out.println(number + " is not a Magic Number.");
}
scanner.close();
}
}
You can also read : What is a strong number in Java and how to code it?
How the Program Works
- Input: The user will enter any random number for which he wants to check the magic number.
- Sum of digits: The sumOfDigits method calculates the sum of the digits of the number.
- Reduce to a single digit: The isMagicNumber method reduces the number to a single digit by summing the digits recursively.
- Check condition: If the final single-digit number is 1, then it’s a magic number.
Example Output
Case 1: Magic Number
Input: 19
Process: 1+9=101 + 9 = 101+9=10, 1+0=11 + 0 = 11+0=1
Output: 19 is a Magic Number!
Case 2: Non-Magic Number
Input: 123
Process: 1+2+3=61 + 2 + 3 = 61+2+3=6
Output: 123 is not a Magic Number.
Why Learn Magic Number Program in Java?
Logic Building: If you are practicing this kind of program, it will improve your problem-solving skills by teaching recursive and iterative approaches.
Practical Use: Practical will help you to know how digit-based algorithms work is essential for competitive programming and designing algorithms.
Fun Concept: Coding on this kind of program is very enjoyable and you will love it.
Tips for Optimizing the Program
Use Recursion: If you are working on a program where while loop is used but you don’t like it and want to simplify the program, replace the while loop with a recursive function.
Edge Cases: Make sure to handle cases such as 0 or any negative numbers, to ensure the program works in all scenarios.
Efficient Input Handling: If you are accepting the input from user, make sure you have placed the validation to prevent invalid or unexpected entries.
Conclusion
The Magic Number Program in Java is an excellent exercise for both beginners and intermediate Java developers and learners, its improvs the logic and concept of numbers manipulation, loops and conditional checks in an engaging way. If you want to become expert on this kind of program, try implementing the program yourself and tweak it to learn even more.
If you like this article and looking for more java articles like this, please explore TecheiTrail.