Automorphic Number in Java

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
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 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.

Automorphic Number in C Programming

Example:

5 =(5)2 = 25

6 = (6)2 = 36

25 = (25)2 = 625

76=(76)2 = 5776

376 = (376)2 = 141376

890625 = (890625)2 = 793212890625

These numbers are the automorphic numbers.

Automorphic Number in C

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

Some other methods to solve the problem :

Method 1 :

Run
public class Main {
public static void main(String[] args) {
    int x=5;
    int y=x*x;
    if(y%10==x%10)
        System.out.println("automorphic");
    else
        System.out.println("not");
}
}

Output :

automorphic

Method 2 :

Run
class HelloWorld {
public static void main(String[] args) {
    int num=10;
    int sqt=num*num;
    int count=0;
    int temp=num;
    while(temp!=0)
    {
        temp=temp/10;
        count++;
    }
    temp=sqt;
    int rem=0,rev=0;
    for(int i=0;i< count;i++)
    {
        rem=temp%10;
        rev=rev*10+rem;
        temp=temp/10;
    }
    if(rev==num)
        System.out.println("Automorphic Number");
    else
        System.out.println("No Automorphic Number");
}
}

Output :

No Automorphic Number

Prime Course Trailer

Related Banners

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

35 comments on “Automorphic Number in Java”


  • BHARATHWAJ P

    public class Main {

    // Function to check if a number is Automorphic
    static boolean isAutomorphic(int n) {
    // Calculate the square of the number
    int square = n * n;

    // Convert number and its square to strings
    String numStr = Integer.toString(n);
    String squareStr = Integer.toString(square);

    // Check if numStr is a suffix of squareStr
    return squareStr.endsWith(numStr);
    }

    public static void main(String[] args) {
    int n = 25; // Change this value to test different numbers

    if (isAutomorphic(n)) {
    System.out.println(n + ” is an Automorphic number.”);
    } else {
    System.out.println(n + ” is not an Automorphic number.”);
    }
    }
    }


  • NISHA V S

    import java.util.*;
    public class Main {
    public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    String s=String.valueOf((n*n));
    System.out.println((s.endsWith(String.valueOf(n)))?”automorphic”:”not automorphic”);
    }
    }


  • NISHA V S

    import java.util.*;
    public class Main {
    public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    System.out.println((Math.pow(n,2)%10 == n)?”automorphic”:”not automorphic”);
    }
    }


  • SAI

    int num=5;
    int sq=num*num;
    int last=sq%10;
    if (num==last)
    System.out.println(“automorphic number”);
    else
    System.out.println(“not automorphic number”);


  • siddeshaob2001

    package pripInsta;

    public class AutomorphicNumber {

    public static void Automorphic(int num)
    {
    int rem,square=num*num;

    if(square!=0)
    {
    rem=square%10;
    if(rem!=num)
    {
    System.out.println(“not Automorphic Number”);
    }
    else
    {
    System.out.println(“is Automorphic Number”);
    }
    }
    }
    public static void main(String[] args) {

    int num=4;
    Automorphic(num);
    }
    }


  • Chetan Mundle

    package prep1;

    import java.util.Scanner;

    public class autoMorphicNumber {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter number : “);
    int num = sc.nextInt();

    int flag = num, rem, howManuNum = 0;
    int sqrRoot = num * num;
    while (flag != 0) {
    rem = flag % 10;
    howManuNum++;
    flag /= 10;
    }

    int i = 1;
    int flag1 = num;
    String temp = “”;
    while (i <= howManuNum) {
    rem = flag1 % 10;
    temp = String.valueOf(rem) + temp;
    flag1 /= 10;
    i++;
    }

    int temp2 = Integer.parseInt(temp);

    if (temp2 == num) {
    System.out.println("Yes, The above number is Automarphic");
    }else {
    System.out.println("No, The above number is Automarphic");
    }

    }
    }


  • Venkatesh

    public class AutomarphicNum {
    public static void main(String[] args)
    {
    int num=25;
    int temp=check(num);
    int sqr=num*num;
    int temp1=check1(sqr,temp);
    if(temp==temp1)
    {
    System.out.println(“automarphic number”);
    }
    else
    {
    System.out.println(“not automarphic”);
    }
    }
    static int check(int num)
    {
    int sum=0;//num=25
    while(num>0)//25>0,2>0
    {
    int rem=num%10;//5,2
    sum=sum+rem;//5+0,5+2
    num=num/10;//2,0
    }
    return sum;
    }
    static int check1(int sqr,int temp)
    {
    int sum=0;
    while(sqr>0)//625>0,62>0
    {
    int rem=sqr%10;//5,2
    sum=sum+rem;//0+5,0+5+2
    if(temp==sum)
    {
    return sum;
    }
    sqr=sqr/10;//62,
    }
    return 0;
    }
    }


  • ankit98thakur

    I just want to inform you that your method1 will not work properly for every number, like for 11 it’s square is “121” which is not Automorphic but it will show Automorphic according to the code written.

    public class Main {
    public static void main(String[] args) {
    int x=5;
    int y=x*x;
    if(y%10==x%10)
    System.out.println(“automorphic”);
    else
    System.out.println(“not”);
    }
    }


  • Vamsi

    public class Main {
    public static void main(String[] args) {
    int x=5;
    int y=x*x;
    if(y%10==x%10)
    System.out.println(“automorphic”);
    else
    System.out.println(“not”);
    }
    }


  • Vamsi

    public class Main {
    public static void main(String[] args) {
    int x=5;
    int y=x*x;
    System.out.println(y%10);
    System.out.println(x/10);

    if(y%10==x%10)
    System.out.println(“automphric”);
    else
    System.out.println(“not”);
    }
    }


  • SRINJAY

    System.out.println((((n*n)%10)==n) ? “Automorphic number” : “Not an automorphic number”);


    • SRINJAY

      System.out.println((((n*n)%10)==(n%10)) ? “Automorphic number” : “Not an automorphic number”);
      Updated for any number!


  • sunaynabhattacharjee00

    class HelloWorld {
    public static void main(String[] args) {
    int num=10;
    int sqt=num*num;
    int count=0;
    int temp=num;
    while(temp!=0)
    {
    temp=temp/10;
    count++;
    }
    temp=sqt;
    int rem=0,rev=0;
    for(int i=0;i<count;i++)
    {
    rem=temp%10;
    rev=rev*10+rem;
    temp=temp/10;
    }
    if(rev==num)
    System.out.println("A");
    else
    System.out.println("No");
    }
    }