Magic Number Program in Java

In computer programming, the term magic number has multiple meanings. In this post, we will discuss the magic number and also develop a Java program to check the given number is a magic number or not.

A number is said to be a magic number if the sum of its digits is calculated till a single digit recursively by adding the sum of the digits after every addition. If the single-digit comes out to be 1, then the number is a magic number. Example of magic numbers are:- 10, 19, 28, 55, 1234 e.t.c.

1054 = 1 + 0 + 5 + 4 = 10 (It is 2 digit number, so again find the sum of digits)
10 = 1 + 0 = 1 (finally sum of digits is 1)
Hence 1054 is a magic number.

Similarly,
25417 = 2 + 5 + 4 + 1 + 7 = 19 (Since it is a two digit number)
19 = 1 + 9 = 10 (Again it is two digit number)
10 = 1 + 0 = 1 (finally sum of digits is 1)
Hence 25417 is a magic number.

import java.util.Scanner;

public class MagicNumber {

   // method to find sum of digits
   // of a given number
   public static int sumOfDigits(int number) {

      int sum = 0;

      while(number != 0) {
         // find last digit
         // add last digit to sum
         sum += (number % 10);

         // remove last digit
         number = number / 10;
      }

      // return sum of digits of the number
      return sum;
   }

   // method to check number is magic number
   public static boolean isMagic(int number) {

      int result = number;

      // find sum of digits until
      // number having single digit
      while(result / 10 != 0) {
        result = sumOfDigits(result);
      }

      return (result==1) ? true : false;
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;

      // read the input 
      Scanner scan =  new Scanner(System.in);
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check the number is magic number or not
      if(isMagic(number))
      System.out.println(number+" is a"
        + " magic number");
      else
      System.out.println(number+" is not a"
              + " magic number");

      // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases are:-

Enter an integer number:: 28
28 is a magic number

Enter an integer number:: 12345
12345 is not a magic number

Also see:- Special numberMagic numberArmstrong numberPerfect numberEvil NumberSpy NumberSunny number in Java

Direct approach

The Sum of digits until a single digit in Java also can be calculated by directly dividing the number by 9. If number is divisible by 9 then it’s Sum of digits until single digit is 9 else it is number % 9

import java.util.Scanner;

public class MagicNumber {

   // method to find digital sum
   public static boolean isMagic(int number) {
      if(number%9 == 1) return true;
      else return false;
   }

  public static void main(String[] args) {

      // declare variables
      int number = 0;

      // read the input 
      Scanner scan =  new Scanner(System.in);
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check the number is magic number or not
      if(isMagic(number))
        System.out.println(number+" is a"
            + " magic number");
      else
        System.out.println(number+" is not a"
              + " magic number");

      // close Scanner class object
      scan.close();
  }
}

Java program to find all magic number in the given range

import java.util.Scanner;

public class MagicNumber {

  // method to find digital sum
  public static boolean isMagic(int number) {
      if(number%9 == 1) return true;
      else return false;
  }

  public static void main(String[] args) {

      // declare variables
      int minRange = 0;
      int maxRange = 0;

      // read the input 
      Scanner scan =  new Scanner(System.in);
      System.out.print("Enter min range:: ");
      minRange = scan.nextInt();
      System.out.print("Enter max range:: ");
      maxRange = scan.nextInt();

      // find all magic numbers
      System.out.println("The magic numbers"
          + " from "+minRange+" to "
         +maxRange+ " are: ");

      for(int i=minRange; i<maxRange; i++) {
         if(isMagic(i))
         System.out.print(i+"\t");
      }

      // close Scanner class object 
      scan.close();
   }
}

The output for the different test-cases are:-

Enter min range:: 1
Enter max range:: 100
The magic numbers from 1 to 100 are:
1 10 19 28 37 46 55 64 73 82 91

Enter min range:: 100
Enter max range:: 200
The magic numbers from 100 to 200 are:
100 109 118 127 136 145 154 163 172 181 190 199

Enter min range:: 1000
Enter max range:: 1111
The magic numbers from 1000 to 1111 are:
1000 1009 1018 1027 1036 1045 1054 1063 1072 1081 1090 1099 1108

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply