# 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.

## 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

1. public class AutomorphicNumberExample1
2. {
3. //user-defined static method that checks whether the number is automorphic or not
4. static boolean isAutomorphic(int num)
5. {
6. //determines the square of the specified number
7. int square = num * num;
8. //comparing the digits until the number becomes 0
9. while (num > 0)
10. {
11. //find the remainder (last digit) of the variable num and square and comparing them
12. if (num % 10 != square % 10)
13. //returns false if digits are not equal
14. return false;
15. //reduce num and square by dividing them by 10
16. num = num/10;
17. square = square/10;
18. }
19. return true;
20. }
21. //Driver code
22. public static void main(String args[])
23. {
24. //number to be check
25. //calling the method and prints the result accordingly
26. System.out.println(isAutomorphic(76) ? “Automorphic” : “Not Automorphic”);
27. System.out.println(isAutomorphic(13) ? “Automorphic” : “Not Automorphic”);
28. }
29. }

Output 1:

```Automorphic
Not Automorphic
```

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

AutomorphicNumberExample2.java

1. import java.util.Scanner;
2. public class AutomorphicNumberExample2
3. {
4. public static void main(String args[])
5. {
6. Scanner in = new Scanner(System.in);
7. System.out.print(“Enter a number to check: “);
8. //reading a number from the user
9. int num = in.nextInt();
10. int count=0;
11. //determines the square of the given number
12. int square = num*num;
13. //copying the variable num into temp
14. int temp = num;
15. //iterate over the variable num until the condition become false
16. while(temp>0)
17. {
18. count++;
19. //removes last digit of the variable num
20. temp=temp/10;
21. }
22. //determines the last digit of the variable square
23. int lastDigit = (int) (square%(Math.pow(10, count)));
24. //compare num with last digit of the variable square
25. if(num == lastDigit)
26. System.out.println(num+ ” is an automorphic number.”);
27. else
28. System.out.println(num+ ” is not an automorphic number.”);
29. }
30. }

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

1. import java.util.Scanner;
2. public class AutomorphicNumberExample3
3. {
4. //user-defined static method that checks whether the number is automorphic or not
5. private static boolean isAutomorphic(int num)
6. {
7. int count=0;
8. //determines the square of the given number
9. int square = num*num;
10. //copying the variable num into temp
11. int temp = num;
12. //iterate over the variable num until the condition become false
13. while(temp>0)
14. {
15. count++;
16. //removes last digit of the variable num
17. temp=temp/10;
18. }
19. //determines the last digit of the variable square
20. int lastDigit = (int) (square%(Math.pow(10, count)));
21. //compare the last digit with num
22. return num == lastDigit;
23. }
24. public static void main(String args[])
25. {
26. Scanner in = new Scanner(System.in);
27. int start, end;
28. System.out.print(“Enter the starting value: “);
29. start = in.nextInt();
30. System.out.print(“Enter the ending value: “);
31. end = in.nextInt();
32. System.out.println(“Automorphic numbers between “+start+” and “+end+” are: “);
33. //the for loop starts from the starting value and execute until the condition becomes false
34. for(int i=start; i<=end; i++)
35. {
36. //calling the user-defined method
37. if(isAutomorphic(i))
38. //prints the number if it is automorphic
39. System.out.print(i+” “);
40. }
41. }
42. }

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```