How to find automorphic numbers?
To determine whether a number is automorphic or not, we need to follow a simple procedure:
1. Square the number (multiply it by itself).
2. Check if the resulting square ends with the original number.
Let's take an example to show this process. Consider the number 376.
Step 1: Square the number.
376^2 = 141,376
Step 2: Check if the square ends with the original number.
The square (141,376) ends with 376, which is the original number.
Therefore, 376 is an automorphic number.
Let’s put this process into a step-by-step algorithm:
1. Take a number as input.
2. Calculate the square of the number.
3. Convert the original number to a string & store its length.
4. Convert the squared result to a string.
5. Check if the squared result ends with the original number by comparing the last digits of the squared result with the original number.
- If the last digits of the squared result match the original number, it is an automorphic number.
- If the last digits of the squared result do not match the original number, it is not an automorphic number.
Java Automorphic Number Program
Now that we understand what automorphic numbers are & how to find them, let's implement a Java program to check if a given number is automorphic or not :
Java
import java.util.Scanner;
public class AutomorphicNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (isAutomorphic(number)) {
System.out.println(number + " is an automorphic number.");
} else {
System.out.println(number + " is not an automorphic number.");
}
}
public static boolean isAutomorphic(int number) {
int square = number * number;
String numberStr = String.valueOf(number);
String squareStr = String.valueOf(square);
return squareStr.endsWith(numberStr);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Enter a number: 6
6 is an automorphic number.
In this program :
1. We import the `Scanner` class to take input from the user.
2. In the `main` method, we create a `Scanner` object & prompt the user to enter a number. We store the input in the `number` variable.
3. We call the `isAutomorphic` method, passing the `number` as an argument. This method will return `true` if the number is automorphic & `false` otherwise.
4. Based on the result of `isAutomorphic`, we print whether the number is an automorphic number or not.
5. In the `isAutomorphic` method, we first calculate the square of the number by multiplying it by itself & store it in the `square` variable.
6. We convert both the original number & the square to strings using `String.valueOf()` & store them in `numberStr` & `squareStr`, respectively.
7. Finally, we use the `endsWith()` method to check if `squareStr` ends with `numberStr`. If it does, we return `true`, indicating that the number is automorphic. Otherwise, we return `false`.
This program provides a simple & efficient way to check if a given number is automorphic or not. The `isAutomorphic` method can be reused for any number, making it a useful utility function.
Examples
Let's look at a few examples to better understand how the Java program works:
Example 1
Enter a number: 25
Output: 25 is an automorphic number.
Explanation
25^2 = 625
The square of 25 (625) ends with 25, so it is an automorphic number.
Example 2
Enter a number: 12
Output: 12 is not an automorphic number.
Explanation
12^2 = 144
The square of 12 (144) does not end with 12, so it is not an automorphic number.
Example 3
Enter a number: 890625
Output: 890625 is an automorphic number.
Explanation
890625^2 = 793212890625
The square of 890625 (793212890625) ends with 890625, so it is an automorphic number.
These examples demonstrate how the Java program correctly identifies whether a given number is automorphic or not. The program works for numbers of various lengths & efficiently checks the automorphic property.
Frequently Asked Questions
Can negative numbers be automorphic?
No, negative numbers cannot be automorphic because their squares are always positive.
Are there any even automorphic numbers?
Yes, there are even automorphic numbers, such as 0, 6, 76, 376, & 90625.
Is every number that ends with 5 an automorphic number?
No, not every number ending with 5 is automorphic. For example, 15 & 35 are not automorphic.
Conclusion
In this article, we learned about automorphic numbers, which are numbers whose squares end with the original number itself. We discussed about the concept, learned how to identify automorphic numbers, & implemented a Java program to check if a given number is automorphic.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.