Magic Number Program in Java with Examples

In this tutorial I will be sharing what is Magic number, examples of Magic number, algorithm and java program to check whether a given number is Magic number or not.

Read Also :  Java Program to Check Bouncy Number  

What is Magic Number

A Magic number is a number whose sum of its digits are calculated till a single digit is obtained by recursively adding the sum of its digits. If single digit obtained is 1, then the number is magic number, otherwise not.
Examples :

Number to check : 19 
1 + 9 = 10        // 10 is not a single digit number, continue adding digits   
1 + 0 = 1         // 19 is a Magic number
 
Number to check : 1891 
1 + 8 + 9 + 1 = 19 // 19 is not a single digit number, continue adding digits   
1 + 9 = 10         // 10 is not a single digit number, continue adding digits 
1 + 0 = 1          // 1891 is a Magic number 

Below are examples of numbers which are NOT Magic numbers

Number to check : 29 
2 + 9 = 11        // 11 is not a single digit number, continue adding digits   
1 + 1 = 2         // 29 is NOT a Magic number
 
Number to check : 2888 
2 + 8 + 8 + 8 = 26// 26 is not a single digit number, continue adding digits   
2 + 6 = 8         // 2888 is NOT a Magic number

Java Program for Magic Number


import java.util.*;
public class JavaHungry {
       
    public static void main(String args[]) {
        System.out.println("Enter any number : ");    
        Scanner scan = new Scanner(System.in);
        int inputNumber = scan.nextInt();
        boolean result = checkMagicNumber(inputNumber);
        if(result)
            System.out.println(inputNumber +" is a Magic Number");          
        else
            System.out.println(inputNumber +" is NOT a Magic Number");
    }
    
    public static boolean checkMagicNumber(int inputNumber) { 
        // Initialize sumOfDigits of inputNumber 
        int sumOfDigits = 0;
        // Create a copy of the inputNumber 
        int number = inputNumber;
 
        while( sumOfDigits > 9 || number > 0) {
        /* if number is zero and sumOfDigits is non-zero
           then the loop will continue.
           Loop will stop when number is zero and
           sumOfDigits becomes a single digit */    
            if (number == 0) {
                number = sumOfDigits;
                sumOfDigits = 0;
            }
            sumOfDigits = sumOfDigits + (number % 10);
            number = number / 10;
        } 
         /* If sumOfDigits is equal to 1 then 
            the number is Magic number, otherwise not */ 
            return (sumOfDigits == 1);
    }
}

Output :
Enter any number : 145
145 is a Magic number

2. Another way of checking whether a given number is Magic number or not is given below.


import java.util.*;
public class JavaHungry {
    public static void main(String args[]) {
        System.out.println("Enter any number : ");    
        Scanner scan = new Scanner(System.in);
        int inputNumber = scan.nextInt();
        if(inputNumber % 9 == 1)
            System.out.println(inputNumber +" is a Magic Number");          
        else
            System.out.println(inputNumber +" is NOT a Magic Number"); 
    }
}


Output : 
Enter any number : 226
226 is a Magic number 

List of Magic numbers from 1 to 100 are :

1, 10, 19, 28, 37, 46, 55, 64, 73, 82, 91, 100

Algorithm for Magic Number  

1. Initialize sumOfDigits variable value to 0. It will represent the sum of digits of a given inputNumber.

2. Create a copy of the inputNumber (original number) by storing its value in variable number.

3. Using while loop
a. Continue the loop till the sumOfDigits does not become a single digit or number is  not equal to zero.
b. Get the rightmost digit of variable number by using (number % 10) and add its value to the variable sumOf Digits.

4. Check whether the variable sumOfDigits is equal to 1.
     a. If both are equal then the inputNumber is Magic number.
     b. Otherwise, the inputNumber is not a Magic number.

That's all for the day, please mention in comments if you know any other way to implement Magic number program in java.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry