Programming Examples
Java program for Automorphic Number
Write a Program in Java to input a number and check whether it is an Automorphic Number or not.
Note: An automorphic number is a number which is present in the last digit(s) of its square.
Example: 25 is an automorphic number as its square is 625 and 25 is present as the last digits
Sample:
Enter a Number : 25
25 is an Automorphic Number.
Enter a Number : 6
6 is an Automorphic Number.
Enter a Number : 9
9 is not an Automorphic Number.
Solution
import java.util.*;
class AutomorphicNumber
{
public static void main(String arr[])
{
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("Number is Automorphic Number");
else
System.out.println("Number is Not an Automorphic Number");
}
}
Output
Enter a Number : 25
25 is an Automorphic Number.
Enter a Number : 6
6 is an Automorphic Number.
Enter a Number : 9
9 is not an Automorphic Number.