Write a Java Program to Check Automorphic Number

In the previous article, we have discussed Java Program to Check Goldbach Number

In this article we are going to understand what Automorphic number is and how we can check whether a number is Automorphic or not in Java with examples.

Java Program to Check Automorphic Number

Automorphic numbers are numbers whose square contains the same digits in the same order as the number itself i.e. the square of the number has the number at its end.

Example:

5 -> (5)2 = 25  Automorphic number
6 -> (6)2 = 36 Automorphic number
9 -> (9)2 = 81 Not an  Automorphic number 

In the above examples the number 5 and 6 square is 25 and 36 respectively. The last digits of both the squares are the number itself, hence they are Automorphic number. However, the square of 9 is 81, which does not contain 9 at its end. Hence 9 is not Automorphic.

Let’s see different ways do it.

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Approach:

  1. We ask the user to enter a number which we store in an integer variable num.
  2. We then use a while loop to count the number of digits in the number and store it in dig.
  3. The number entered by the user is then squared and stored in squaredNum.
  4. The number is then divided by 10^dig and the remainder is stored in squaredNumRemainder and is compared against the number.
  5. If both of them are same it is said to be an Automorphic number or else it is not.

Method-1: Java Program to Check Automorphic Number By Using Static Value

import java.util.Scanner;
import java.lang.Math;

public class AutomorphicNumber
{
    public static void main(String args[])
    {
        //A Number is declared
        int num = 6;
        
        int squaredNum, temp, squaredNumRemainder, dig = 0;
        //Storing the number in a temporary variable to preserve original value
        temp = num;
        //Loop that counts the number of digits in a number
        while(temp>0)
        {
            temp = temp / 10;
            dig++;
        }

        //Finding the square of the number by using library function Math.pow()
        squaredNum = (int)Math.pow(num,2);
        squaredNumRemainder = squaredNum%(int)Math.pow(10, dig);
        if(squaredNumRemainder==num)
        {
            System.out.print(num+" is an Automorphic number");            
        }
        else
            System.out.print(num+" is not an Automorphic number");
    }
}
Output:

6 is an Automorphic number

Method-2: Java Program to Check Automorphic Number By Using User Input Value

import java.util.Scanner;
import java.lang.Math;

public class AutomorphicNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number :");
        int num = scan.nextInt();
        
        int squaredNum, temp, squaredNumRemainder, dig = 0;
        //Storing the number in a temporary variable to preserve original value
        temp = num;
        //Loop that counts the number of digits in a number
        while(temp>0)
        {
            temp = temp / 10;
            dig++;
        }

        //Finding the square of the number by using library function Math.pow()
        squaredNum = (int)Math.pow(num,2);
        squaredNumRemainder = squaredNum%(int)Math.pow(10, dig);
        if(squaredNumRemainder==num)
        {
            System.out.print(num+" is an Automorphic number");            
        }
        else
            System.out.print(num+" is not an Automorphic number");
    }
}
Output:

Enter a number : 6
6 is an Automorphic number

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: