java program for Automorphic Number

Programming Example

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.

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad