00:00

Java Program to Check Automorphic Number

Last updated:

An automorphic number is a number whose square ends with the number itself.

  • 5 is automorphic because 5² = 25.
  • 76 is automorphic because 76² = 5776.
  • 625 is automorphic because 625² = 390625.
  • 7 is not automorphic because 7² = 49.

1. Check an Automorphic Number Using Arithmetic

If a number has d digits, the last d digits of its square are square % 10d. The program counts the digits, builds that power of 10, and compares. The square is stored in a long so it does not overflow.

AutomorphicNumber.java
public class AutomorphicNumber {

    static boolean isAutomorphic(int number) {
        long square = (long) number * number;

        // 10 to the power of the number of digits
        long divisor = 1;
        for (int temp = number; temp > 0; temp /= 10) {
            divisor *= 10;
        }
        if (number == 0) {
            divisor = 10;
        }
        return square % divisor == number;
    }

    public static void main(String[] args) {
        int[] numbers = {5, 76, 125, 625, 7};

        for (int n : numbers) {
            System.out.println(n + " (square " + ((long) n * n) + ") is "
                    + (isAutomorphic(n) ? "an automorphic number." : "not an automorphic number."));
        }
    }
}
Output
5 (square 25) is an automorphic number.
76 (square 5776) is an automorphic number.
125 (square 15625) is not an automorphic number.
625 (square 390625) is an automorphic number.
7 (square 49) is not an automorphic number.

2. Check an Automorphic Number with User Input

A shorter way is to compare the text of the numbers: the square is automorphic if its string ends with the number's string. String.endsWith() does the comparison.

AutomorphicInput.java
import java.util.Scanner;

public class AutomorphicInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        scanner.close();

        long square = (long) number * number;
        boolean automorphic = String.valueOf(square).endsWith(String.valueOf(number));

        System.out.println(number + "² = " + square);
        System.out.println(automorphic ? "It is an automorphic number." : "It is not an automorphic number.");
    }
}
Output 1
Enter a number: 625
625² = 390625
It is an automorphic number.
Output 2
Enter a number: 85
85² = 7225
It is not an automorphic number.

3. Print Automorphic Numbers in a Range

Using the same check in a loop finds every automorphic number between two limits.

AutomorphicRange.java
public class AutomorphicRange {

    static boolean isAutomorphic(long n) {
        return String.valueOf(n * n).endsWith(String.valueOf(n));
    }

    public static void main(String[] args) {
        int start = 1, end = 10000;

        System.out.println("Automorphic numbers from " + start + " to " + end + ":");
        for (int n = start; n <= end; n++) {
            if (isAutomorphic(n)) {
                System.out.print(n + " ");
            }
        }
        System.out.println();
    }
}
Output
Automorphic numbers from 1 to 10000:
1 5 6 25 76 376 625 9376

4. Summary

  • An automorphic number appears at the end of its own square, such as 76² = 5776.
  • Compare square % 10d with the number, where d is its number of digits.
  • Or convert both to strings and use endsWith().
  • Store the square in a long to avoid overflow for larger numbers.

Related Programs