Find automorphic number in java

In this post, we will see how to find automorphic number in java.
[toc]
Automorphic number are those natural numbers whose square ends in same digits are number itself.

25 is automorphic number because:
square of 25 is 625 and 625 ends with same digits as original number 25.

76 is automorphic number because:
square of 76 is 5776 and 5776 ends with same digits as original number 76.

Find automorphic number using digits

Here is simple algorithm to find automorphic number using digits.

  • Take value of n from user using Scanner class
  • Find square of n sqrOfN
  • Count number of digits noOfDigits in n
  • Find last noOfDigits of sqrOfN and assign it to lastNDigitsOfSquare
  • Check if lastNDigitsOfSquare is equal to n
    • If yes, then number is automorphic
    • If no, then number is not automorphic
package org.arpit.java2blog;

import java.util.Scanner;

public class AutomorphicNumberMain {

    public static void main(String args[]){

        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number");
        int n = in.nextInt();

        boolean isAutomorphicNumber = isAutomorphicNumber(n);
        if(isAutomorphicNumber)
        {
            System.out.println(n +" is automorphic number");
        }
        else
        {
            System.out.println(n +" is not automorphic number");
        }
    }

    private static boolean isAutomorphicNumber(int n) {
        int noOfDigits=0;
        int sqrOfN = n * n;
        System.out.println("Square of n is: "+sqrOfN);
        int temp = n;  //copying num

        // count digits of num
        while(temp>0){
            noOfDigits++;
            temp=temp/10;
        }

        int lastNDigitsOfSquare = (int) (sqrOfN%(Math.pow(10,noOfDigits)));

        if(n == lastNDigitsOfSquare)
            return true;
        else
           return false;
    }
}

Output:

Enter a number
76
Square of n is: 5776
76 is automorphic number

Find automorphic number using String

We can also use String’s endsWith() method to find automorphic number using String.
Here is simple algorithm to find automorphic number using digits.

  • Take value of n from user using Scanner class and convert it to String numStr
  • Find square of n and convert it to String sqrNumStr
  • Check if sqrNumStr with numStr
    • If yes, then number is automorphic
    • If no, then number is not automorphic
package org.arpit.java2blog;

import java.util.Scanner;

public class AutomorphicNumberUsingString {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number");
        int n = in.nextInt();

        boolean isAutomorphicNumber = isAutomorphicNumberUsingString(n);
        if(isAutomorphicNumber)
        {
            System.out.println(n +" is automorphic number");
        }
        else
        {
            System.out.println(n +" is not automorphic number");
        }
    }

    // Check automorphic number using String
    public static boolean isAutomorphicNumberUsingString(int n) {

        // convert numbers to string
        String numStr = Integer.toString(n);
        String sqrNumStr = Integer.toString(n*n);
        System.out.println("Square of n is: "+sqrNumStr);
        // check endWith
        if(sqrNumStr.endsWith(numStr))
            return true;

        return false;
    }
}

Output:

Enter a number
25
Square of n is: 625
25 is automorphic number

Find automorphic number in range

We can simply use a for loop to iterate from minimum to maximum range interval and check if number is automorphic or not.

package org.arpit.java2blog;

import java.util.Scanner;

public class AutomorphicNumberRange {

    public static void main(String[] args) {
        int min = 0, max = 0;

        Scanner scanner = new Scanner(System.in);

        // Get min and max values
        System.out.print("Enter minimum range: ");
        min = scanner.nextInt();
        System.out.print("Enter maximum range ");
        max = scanner.nextInt();

        // check number
        System.out.println("The Automorphic numbers from "+
                min+" to "+ max+" are: ");

        for(int i=min; i<=max; i++) {
            if(isAutomorphicNumber(i))
                System.out.print(i+" ");
        }

        // close Scanner class object
        scanner.close();
    }

    private static boolean isAutomorphicNumber(int n) {
        int noOfDigits=0;
        int sqrOfN = n * n;
        int temp = n;  //copying num

        // count digits of num
        while(temp>0){
            noOfDigits++;
            temp=temp/10;
        }

        int lastNDigitsOfSquare = (int) (sqrOfN%(Math.pow(10,noOfDigits)));

        if(n == lastNDigitsOfSquare)
            return true;
        else
            return false;
    }
}

Output:

Enter minimum range: 5
Enter maximum range 100
The Automorphic numbers from 5 to 100 are:
5 6 25 76

That’s all about how to find automorphic number in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.