# Automorphic Number in Java

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 141**376**. 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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.