How to find Magic Number in Java

In this tutorial we will learn what is magic number and how to find magic number in java . We will write magic number program in java to check given number is magic or not .

What is the magic number ?

Magic number is the if the sum of its digits recursively are calculated till a single digit If the single digit is 1 then the number is a magic number. Magic number is very similar with Happy Number.  for example 226 is a number then

step 1 : sum of each number is 2+2+6=10

step 2: sum of 10 is 1+0 = 1 Because single digit is 1 so given number 226 is magic number .

Algorithm for Magic Number

Step 1 : Take the input from user num to check magic number and initialize sum with 0 .

Step 2 : Now store sum of digits in sum till number > 0 or sum > 9

Step 3 : When number become 0 then replace number with sum of digits and set sum =0

Step 4: Now again calculate sum of digits of number and store in sum variable .

Step 5 : If sum is equal to 1 then given number is magic number .

Step 6 : If sum is not equal to 1 then given number is not magic number .

Magic number program in java

Now We will write magic number program in java . We will take input from user and then implement above algorithm to find magic no in java .Let’s see java program

import java.util.Scanner;

public class MagicNumber{
 
    public static void main(String[] args) { 
        int num , sum=0;
	Scanner sc = new Scanner(System.in);
	System.out.print("enter a number: " );
	num =sc.nextInt();
		
        while (num > 0 || sum > 9) 
        { 
            if (num == 0) 
            { 
            	num = sum; 
                sum = 0; 
            } 
            sum += num % 10; 
            num /= 10; 
        } 
 
        // If sum = 1, then it is magic number 
        if(sum == 1) {
            System.out.println("It is a magic number");
        }else {
            System.out.println("It is not a magic number");
        }
    }
}

Output:

enter a number: 226
It is a magic number

Efficient Approach to find Magic Number in Java

There is another approach to find Magic Number In Java . In this approach, we can get Magic Number after dividing a number by 9. This is a shortcut method to verify Magic Number. If we get remainder 1 after dividing a number by 9 then the number is a magic number. It is based on the concept of divisibility rule of 9 which says that if a number is divisible by 9, then sum of its all digits are also divisible by 9. An addition of 1 in the original number will increase the ultimate value by 1, making it 10 and the ultimate sum will be 1, which makes a number a magic number.

import java.util.Scanner;

public class MagicNumber2 {
	public static void main(String[] args)
	{
	     
        int num;
		Scanner sc = new Scanner(System.in);
		System.out.print("enter a number: " );
		num =sc.nextInt();
		
	    // Condition to check Magic number
	    if (num % 9 == 1)
	        System.out.printf("Magic Number");
	    else
	        System.out.printf("Not a Magic Number");
	}
	}

Output :

enter a number: 226
Magic Number

In this tutorial we have learned what is the magic number and write magic number program in java . We Find magic no in java using two method here .

Leave a Reply

Your email address will not be published. Required fields are marked *

38 + = 48