Automorphic Number Program in Java

In this post, we will develop an automorphic number program in Java. First, we will learn what is the automorphic number and then we will see what are the various ways to check the number is an automorphic number or not.

In mathematics, a number is called an Automorphic number if the square of the number ends with the same number. Example of Automorphic numbers are:- 5, 6, 25, 76, e.t.c..

The square of 6 = 36
The number 36 ends with 6 so it is an automorphic number.

Similarly, the square of the number 76 = 5776
Here the 5776 ends with 76 so, 76 is an automorphic number.

There are two ways to check the given number is an automorphic number or not
1) By using string
2) Without using string

The first way i.e. using string is very simple, so we will discuss it later. First, we will develop the program without using pre-defined methods of the String class.

Procedure

Procedure to develop the method to check number is automorphic or not without using string class methods,

  1. Take a number
  2. Calculate its square and store it in a variable
  3. Find the last digit of number and square
  4. Compare both last digits
    • If they are not the same then they are not an automorphic number.
    • If they are same then go to the next step
  5. Remove the last digit of number and square
  6. Repeat 4 to 6 step until the number becomes 0
  7. All previous step completed successfully so, the number is an automorphic number
// checks number is automorphic number or not
// without using String class methods
public static boolean isAutomorphic(int num) {

   // declare variables
   int square = num*num;
   int lastDigitOfNumber = 0, lastDigitOfSquare = 0;

   // traverse all digits of number
   // and compare it with square
   while(num != 0) {

      // find last digit
      lastDigitOfNumber = num % 10;
      lastDigitOfSquare = square % 10;

      // compare both last digit
      if(lastDigitOfNumber != lastDigitOfSquare)
          return false;

      // remove last digit
      num /= 10;
      square /= 10;
   }

   return true;
}

The above code may look like heavy code for you, but we reduce the number of the line. We can find the last digit and compare the digit in one line. The below method demonstrate it,

Automorphic number program in java using while loop

// checks number is automorphic number or not
// without using String class methods
public static boolean isAutomorphic(int num) {
   int square = num*num;

   // traverse all digits of number
   // and compare it with square
   while(num != 0) {

      // find last digit and
      // compare both last digit
      if((num % 10) != (square % 10))
          return false;

      // remove last digit
      num /= 10;
      square /= 10;
   }

   return true;
}

Automorphic number program in java using for loop

public static boolean isAutomorphic(int num) {

  for(int square = num*num; num!=0; num/=10, square/=10)
     if((num % 10) != (square % 10))
          return false;

  return true;
}

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

Java program to check automorphic number without string

Based on the above-discussed methods we will write a Java program to check the number is an automorphic number or not. We will use the while loop because it is simplified with respect to for loop and everyone can understand.

import java.util.Scanner;

public class AutomorphicNumber {

   // Checks number is automorphic number or not
   // without using String class methods
   public static boolean isAutomorphic(int num) {
      int square = num*num;

      // traverse all digits of number
      // and compare it with square
      while(num != 0) {

         // find last digit and
         // compare both last digit
         if((num % 10) != (square % 10))
            return false;

         // remove last digit
         num /= 10;
         square /= 10;
      }

      return true;
   }

   public static void main(String[] args) {
      // declare variables
      int number = 0;
      boolean result = false;

      //create Scanner class object to take input
      Scanner scan = new Scanner(System.in);

      // take input from end-user
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check number is Automorphic number or not
      result = isAutomorphic(number);
      if(result)
          System.out.println(number +
                  " is an Automorphic number.");
      else
          System.out.println(number +
                  " is not an Automorphic number");
      // close Scanner class object
      scan.close();
   }
}

The output of different test cases:-

Enter an integer number:: 5
5 is an Automorphic number.

Enter an integer number:: 7
7 is not an Automorphic number

Enter an integer number:: 25
25 is an Automorphic number.

Using string

In this case, convert both numbers and the square value of the number into the string using toString() method.

// convert numbers to string
String str_num = Integer.toString(num);
String str_square = Integer.toString(num*num);

Now, using endswith() method of string class check the string str_square is ends with str_num or not? If yes then the given number is an automorphic number else it is not an automorphic number.

if(str_square.endsWith(str_num)) 
   return true; // automorphic number
else 
   return false; // not an automorphic number

The below program demonstrate it,

import java.util.Scanner;

public class AutomorphicNumberUsingString {

   // Checks number is automorphic number or not
   // using String 
   public static boolean isAutomorphic(int num) {

      // convert numbers to string
      String str_num = Integer.toString(num);
      String str_square = Integer.toString(num*num);

      // check endWith 
      if(str_square.endsWith(str_num)) 
          return true;

      return false;
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;
      boolean result = false;

      //create Scanner class object to take input
      Scanner scan = new Scanner(System.in);

      // take input from end-user
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check number is Automorphic number or not
      result = isAutomorphic(number);
      if(result)
          System.out.println(number +
                  " is an Automorphic number.");
      else
          System.out.println(number +
                  " is not an Automorphic number");

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

Java program for the automorphic number in a range

We can find all automorphic numbers which belong to a given range, for this purpose we will ask the minimum and maximum value of the range from the end-user.

import java.util.Scanner;

public class AutomorphicNumberInRange {

   // Checks number is automorphic number or not
   public static boolean isAutomorphic(int num) {

      for(int square=num*num;num!=0;num/=10,square/=10)
          if((num % 10) != (square % 10)
              return false;

      return true;
   }

   public static void main(String[] args) {

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

      // create Scanner class object
      Scanner scan = new Scanner(System.in);

      // read inputs
      System.out.print("Enter min value of range:: ");
      minRange = scan.nextInt();
      System.out.print("Enter max value of range:: ");
      maxRange = scan.nextInt();

      // check number 
      System.out.println("The Automorphic numbers from "+
              minRange+" to "+ maxRange+" are:: ");

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

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

The output of different test-cases:-

Enter min value of range:: 1
Enter max value of range:: 100
The Automorphic numbers from 1 to 100 are::
1 5 6 25 76

Enter min value of range:: 100
Enter max value of range:: 100000
The Automorphic numbers from 100 to 100000 are::
376 625 9376

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

Leave a Comment

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