Automorphic Number Program in Java

In this section, we will learn automorphic numbers with examples and also create Java programs that check whether the number is automorphic or not.

What is an automorphic number?

A number is called an automorphic number if and only if the square of the given number ends with the same number itself. For example, 25, 76 are automorphic numbers because their square is 625 and 5776, respectively and the last two digits of the square represent the number itself. Some other automorphic numbers are 5, 6, 36, 890625, etc.

Automorphic Number Program in Java

How to find automorphic number?

Follow the steps given below:

  1. Read a number (num) from the user.
  2. Find the square of the given number and store it in a variable (square).
  3. Find the last digit(s) of the square.
  4. Compare the last digit(s) of the variable with num.
    • If they are not equal, the given number is not an automorphic number.
    • If they are the same, go to the next step.
  5. Remove the last digit of the given number i.e. num.
  6. Repeat steps 4 to 6 until the given number becomes 0.

Java Automorphic Number Program

AutomorphicNumberExample1.java

Output 1:

Automorphic
Not Automorphic

Let's see another logic to check the number is automorphic or not.

AutomorphicNumberExample2.java

Output 1:

Enter a number to check: 625
625 is an automorphic number.

Output 2:

Enter a number to check: 312
312 is not an automorphic number.

Let's create a Java program that determines all the automorphic numbers within a specified range.

AutomorphicNumberExample3.java

Output:

Enter the starting value: 1
Enter the ending value: 10000
Automorphic numbers between 1 and 10000 are: 
1 5 6 25 76 376 625 9376

Next TopicJava Atomic