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.
How to find automorphic number?
Follow the steps given below:
- Read a number (num) from the user.
- Find the square of the given number and store it in a variable (square).
- Find the last digit(s) of the square.
- 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.
- Remove the last digit of the given number i.e. num.
- Repeat steps 4 to 6 until the given number becomes 0.
Java Automorphic Number Program
AutomorphicNumberExample1.java
Output 1:
Let's see another logic to check the number is automorphic or not.
AutomorphicNumberExample2.java
Output 1:
Output 2:
Let's create a Java program that determines all the automorphic numbers within a specified range.
AutomorphicNumberExample3.java
Output: