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

28 comments on “Automorphic Number in Java”


  • 63_sahil

    /*
    * Example
    Input : 5
    Output : 25
    Explanation : Number = 5
    when squared you get 25
    as 25 ends with 5
    */

    public class Automorphic {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    double n=5;

    double square=Math.pow(n, 2);

    if(square%10==n) {
    System.out.println(“It is Automorphic”);
    }
    else {
    System.out.println(“It is not”);
    }

    }

    }


  • Kikee

    int num=n;

    int sqrt=n*n;
    int count=0;
    while(num!=0){
    num=num/10;
    count++;
    }
    double rem=sqrt%(Math.pow(10,count));
    if(num==rem)
    {
    return “Not Automorphic”;
    }
    return “Automorphic”;


  • Pravin

    public class Automorphic_Number {
    public static void main(String[] args) {
    int num = 376;
    int sqr = (int) Math.pow(num, 2);
    String num1 = Integer.toString(num);
    String sqr1 = Integer.toString(sqr);
    if (sqr1.endsWith(num1)) {
    System.out.println(“The given number ” + num + ” is an Automorphic Number”);
    } else
    System.out.println(“The given number ” + num + ” is not an Automorphic Number”);
    }
    }


  • 1hk19cs122

    public class Main
    {
    public static void main(String[] args) {
    int n = 35 ;
    int n1=n%10;
    int sq = n*n;
    int result=sq%10;
    if(result==n1){
    System.out.println(“It ia an automorphic”);
    }else{
    System.out.println(“It ia not automorphic”);
    }
    }
    }
    //This is the short code for automorphic.


  • Anand

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {

    Scanner S = new Scanner(System.in);
    int num = S.nextInt();

    //AUTOMORPHIC NUMBER – Number whose square ends with same number. example: 5 – 25

    int square = num*num;
    if(num == rem(square)){
    System.out.println(“Automorphic number”);
    }
    else
    System.out.println(“Not Automorphic”);

    }

    public static int rem(int num){
    int rem = num%10;

    return rem;
    }

    }


  • 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();
    }
    }


    • soumyaranjan

      if number = 25
      then it’s square will be 625
      and remainder = 625 % 10 is 5
      but we need 25 then how do you compare remainder with number.
      Check the definition of Automorphic number
      Automorphic number is a number whose square ends in the same digits as the number itself.
      So in case of 25 we need remainder 25 then you can compare it with number.But in your code remainder will be 5.
      Please correct your code…It will only work on 5 and 6 means only single digit number.


    • Krant

      hey aditi here we get only last digit of the number ….what if we have a 2 digit number then how can you compare it?


    • pavan

      import java.util.Random;
      import java.util.Scanner;
      public class Main{
      public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      Random rand = new Random();
      int count=0,rem,temp,i;
      System.out.println(“ENter a number :”);
      int n = scan.nextInt();
      temp=n;
      while(n>0)
      {
      n=n/10;
      count++;
      }
      int sq = temp*temp;
      int re = (int) (sq%Math.pow(10,count));
      if(temp==re)
      System.out.println(“ATMP”);
      else
      System.out.println(“NO ATMP”);
      }
      }


  • 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