Automorphic number or not using Java

java program to find automorphic number or not

Check Whether Or Not the Number is an Automorphic Number in Java

Given an integer input, the Objective is to check whether the square of the number ends with the same number or not. Therefore, we’ll write a code to Check Whether or Not the Number is an Automorphic Number in Java Language.

Example
Input : 5
Output : Yes, it's an Automorphic Number

Check Whether or Not the Number is an Automorphic Number in Java

Given an integer input, the objective is to check whether or not the Number is an automorphic number or not. To do so we’ll check whether the square if the number ends with the number itself of not. For a number to be Automorphic, it’s square has to end with the number itself. Let’s implement the above mentioned logic in Java Language.

Java Code

Run
public class Main
{
	public static void main(String[] args) {
	   
	int n = 376, sq = n * n ;
    if(isAutomorphic(n) == 1)
        System.out.println("Num: "+ n + ", Square: " + sq + " - is Automorphic");
    else
        System.out.println("Num: "+ n + ", Square: " + sq + " - is not Automorphic");
	   
		
	}
	
	static int isAutomorphic(int n){
    int square = n * n;
    while(n != 0)
    {
        // means not automorphic number
        if(n % 10 != square % 10){
            return 0;
        }
        // reduce down numbers
        n /= 10;
        square /= 10;
    }
    // if reaches here means automorphic number
    return 1;
}
}

Output

Num: 376, Square: 141376 - is Automorphic

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 150+ courses offered by PrepInsta in One Subscription

8 comments on “Automorphic number or not using Java”


  • Soumen

    public class Main {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();
    while (T– > 0) {
    int Num = sc.nextInt();
    int Square = Num * Num;
    int s2 = Square;
    int temp = Num;
    int reverse1 = 0;
    int reverse2 = 0;
    while (temp != 0) {
    int rem = temp % 10;
    int rem2 = s2 % 10;
    reverse1 = reverse1 * 10 + rem;
    reverse2 = reverse2 * 10 + rem2;
    temp /= 10;
    s2 /= 10;
    }
    if (reverse1 == reverse2)
    {
    System.out.println(“Automorphic number”);
    }
    else
    {
    System.out.println(“Not a Automorphic number”);
    }
    }
    }
    }


  • Imamuddin

    import java.util.*;
    public class Main {
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    System.out.print(isautomorphic(n));

    }
    public static boolean isautomorphic(int n)
    {
    int sq=n*n;
    while(n!=0)
    {
    if(n%10!=sq%10)
    {
    return false;
    }
    n=n/10;
    sq=sq/10;
    }
    return true;
    }
    }


  • adityarajsingh1878610003

    public class Automorphic {
    public static void main(String[] args) {
    int n = 376;
    String count = Integer.toString(n); //Convert “n” to string to count the digit
    int r = count.length(); //Counted the number and Stored it in r
    double div = Math.pow(10,r); //here we will get the divisor as per the digit of given
    double res = Math.pow(n,2);
    if(res%div==n){ // here comes the main purpose of div .if digit is 2 ,it will become 100,digit is 3 it will become 1000 and so on…
    System.out.println(“automorphic”);
    }
    else{
    System.out.println(“Not automorphic”);
    }
    }
    }


  • Kanchan

    import java.util.Scanner;
    class AutomorphicNumber
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    int num = scan.nextInt();
    int n=num;
    int sqrt = n*n;
    int count=0;
    while(n!=0)
    {
    n=n/10;
    count++;
    }
    double rem = sqrt % (Math.pow(10,count));
    if(num==rem)
    {
    System.out.print(“Automorphic number”);
    }
    else
    {
    System.out.print(“Not Automorphic”);
    }
    }
    }


  • soumyaranjan

    import java.io.*;
    public class Check_Automorphic_Number {

    public static void main(String[] args)throws IOException {
    BufferedReader ob = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(“Enter a number:”);
    int Number = Integer.parseInt(ob.readLine());
    int Square = Number * Number;
    int rev_Square =0;
    int rev_Number =0;
    while (Square>= Number && Number !=0) {
    int rem_Square = Square % 10;
    rev_Square = rev_Square * 10 + rem_Square;
    int rem_Number = Number % 10;
    rev_Number = rev_Number * 10 + rem_Number;
    Square = Square/10;
    Number = Number / 10;
    }
    if (rev_Square == rev_Number) {
    System.out.println(“This is an automorphic number.”);
    }else{
    System.out.println(“This is not an automorphic number”);
    }

    }
    }


  • Aditi

    import java.util.*;
    public class AutomorphicNumber {
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    int number=sc.nextInt();
    int result= number*number;
    int remainder =result%10;
    if(remainder==number) System.out.print(“Automorphic”);
    else System.out.print(“Not Automorphic”);
    sc.close();
    }
    }


  • Mohit

    what if we just check whether numbers unit place is either 0,1 or 5 or 6 and if yes then directly declare it as automorphic number