An Automorphic number is a number whose square ends with the same digits as the original number.
E.g – 25, 76, 376 etc.
Steps to Check Automorphic Number :
- Take a number as input (num).
- Square the number (sqr).
- Count the number of digits of (num) using while loop (c).
- Compare the last (c) digits of (sqr) with the (num).
- If they are equal then the number is Automorphic else not.
Program:
import java.util.*;
public class Automorphic
{
public static void main(String args[])
{
int n,c=0,sqr=1,temp,lastSquareDigits;
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number");
n= sc.nextInt();
sqr = n*n;
temp=n;
while(temp>0)
{
c++;
temp=temp/10;
}
lastSquareDigits=(int)(sqr%(Math.pow(10,c)));
if(n == lastSquareDigits)
{
System.out.println("Automorphic number");
}
else
System.out.println("Not an Automorphic number");
}
}
Output:
Enter a number
5
Automorphic number
Enter a number
12
Not an Automorphic number