Automorphic numbers are those numbers that when squared have the exact same end digits as the number.
For example, the number 376.
Now the square of the number, 376 is 141376. The last three digits of the square are exactly like the number themselves. Hence, 376 is an Automorphic Number.
However, let the number is 45.
Now the square of the number, 45 is 2025. The last two digits of the square (25) are not same as the number 45. Hence, 45 is NOT an Automorphic Number.
Below is a program that inputs a number, and checks whether it is an automorphic number or not.
Program to Check Automorphic Number in Java
import java.util.Scanner; public class AutomorphicNumber { static void checkForAutomorphic(int number) { int square = number*number; while(number != 0) { if (number % 10 != square % 10) { System.out.println("The number is not an Automorpic number"); return; } number = number/10; square = square/10; } System.out.println("The number is an Automorpic number"); } public static void main (String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a Number: "); int number = input.nextInt(); checkForAutomorphic(number); } }
Output:
Enter a Number: 376
The number is an Automorpic number
The program runs a while loop in which the unit’s digit of the number and the square is compared (number % 10), until all the digits in the number are exhausted. The unit’s digit of the number and the square are removed in every iterance.