**Problem:** Write a Java program to check whether a given number is an Automorphic number or not.

An Automorphic number is that number whose square ends in the same digits as the number itself.

Examples: 25, 76, 376, etc.

## Steps to Check Automorphic Number in Java

- Input a number
.*(num)* - Square the number
.*(sqr)* - Count the number of digits of
using while loop*(num)*.*(c)* - Compare the last
digits of*(c)*with the*(sqr)*.*(num)* - If they are equal then the number is Automorphic else not.

## Check Whether a number is an Automorphic Number in Java

```
import java.util.Scanner;
public class Automorphic {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int num = in.nextInt();
int c=0, sqr = num*num;
int temp =num; //copying num
//countint digits of num
while(temp>0){
c++;
temp=temp/10;
}
int lastSquareDigits = (int) (sqr%(Math.pow(10,c)));
if(num == lastSquareDigits)
System.out.println("Automorphic number");
else
System.out.println("Not an Automorphic number");
}
}
```

**Output**:

`Enter a number
376
Automorphic number
Enter a number
22
Not an Automorphic number`

## Java Program to Find all Automorphic Numbers in the Interval

Using the above program we can only check a single number, whether it is Automorphic or not.

To check and find all automorphic numbers in a given range, we need to define a function that will return true or false based on whether the number passed as parameter is automorphic or not.

Using a loop, we will iterate through all the numbers in the given interval and call the function for each number as an argument.

```
import java.util.Scanner;
public class Automorphicrange {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int l, h;
System.out.println("Input lower interval value");
l = in.nextInt();
System.out.println("Input upper interval value");
h = in.nextInt();
System.out.println("Automorphic numbers between "+l+" and "+h);
for(int i=l; i<=h; i++){
if(checkAutomorphic(i))
System.out.print(i+" ");
}
}
private static boolean checkAutomorphic(int num) {
int c=0, sqr = num*num;
int temp =num; //copying num
//countint digits of num
while(temp>0){
c++;
temp=temp/10;
}
int lastSquareDigits = (int) (sqr%(Math.pow(10,c)));
return num == lastSquareDigits;
}
}
```

**Output**:

`Input lower interval value
1
Input upper interval value
100
Automorphic numbers between 1 and 100
1 5 6 25 76`

Any Doubts comment below.

Can u please explain the process I can’t understand 😞🙏😔

To check if a number is automorphic or not, we first need to count the number of digits of the input number. For ex: 76 has 2 digits.

To count the digits we divide the number by 10 and at the same time increment ‘c’ which was initially 0. After some iterations the number will reduce to 0 and cannot be divided by 10 (76/10=7 -> 7/10=0 ). At this point the value of c will be the number of digits in the input number.

Now we know the count (c) of digits of the input number, we will use it to extract last ‘c’ digits from the ‘square of the input number’.

To extract last ‘c’ digits we will use

`sqr%(Math.pow(10,c)`

.Suppose 76 is the input, its square is 5776. Using while loop we got c=2. To extract last 2 digits of 5776 we will divide it by 100 (math.pow(10,2)). The remainder will get us last two digits(5776%100=76).

Now compare the last two digits with the input value i.e 76 == 76

True so automorphic number.

List of automorphic numbers from 100 to 999

Soumodeep IAM Aadil here

I also didn’t understand

The println is wrong it shd be the opposite way

What is” temp” in programme to check an automorphic number

temporary