easyNumber ProblemsJava

Check Whether a Number Is an Automorphic Number

Decide whether a number is automorphic by testing if its square ends with the number.

Quick Answer

A number is automorphic when its square ends with the number itself. Square n, count how many digits n has (say d), then keep the last d digits of the square with square % 10^d and compare them to n. For 25, the square is 625 and its last two digits are 25, so 25 is automorphic.

Problem Statement

An automorphic number is a non-negative integer whose square ends with the same digits as the number itself. Given n, decide whether it is automorphic and return true or false.

For example, 25 squared is 625, and the last two digits of 625 are 25, so 25 is automorphic. 76 squared is 5776, ending in 76, so it is too.

Input: A single non-negative integer n.

Output: A boolean: true if n is automorphic, otherwise false.

Examples

Example 1
Input:  25
Output: true

25 squared is 625, whose last two digits (25) equal the number.

Example 2
Input:  7
Output: false

7 squared is 49, and its last digit 9 is not 7.

Constraints

  • 0 <= n and n fits in a 32-bit int
  • Use a long for the square to avoid overflow

Think Before You Code

Reveal the questions to ask yourself first
  • How many trailing digits of the square do you need to compare?
  • How do you keep exactly the last d digits of a number?
  • Why should the square be stored in a long rather than an int?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
The number of digits in `n` tells you how many trailing digits of the square matter.
Hint 2
Keeping the last `d` digits is just `square % 10^d`.
Hint 3
Compute `square = (long) n * n`, count digits `d`, then return `square % pow10(d) == n`.

Approach

Reveal the step-by-step approach

Compare the tail of the square with the original number.

  1. Compute square = (long) n * n using a long so it cannot overflow.
  2. Count the digits d of n.
  3. Compute 10^d (as a long).
  4. Take square % 10^d — the last d digits of the square.
  5. Return whether that equals n.

Because you compare exactly as many trailing digits as n has, the check is precise for any valid input.

Dry Run

Walk through the example step by step

Checking n = 25:

square = 25 * 25 = 625
digit count of 25 = 2
10^2 = 100
last 2 digits = 625 % 100 = 25
25 == 25  ->  true

Solution

Reveal the full Java solution
public class AutomorphicNumber {
    public static boolean isAutomorphic(int n) {
        long square = (long) n * n;
        int digits = countDigits(n);
        long lastDigits = square % (long) Math.pow(10, digits);
        return lastDigits == n;
    }

    private static int countDigits(int n) {
        if (n == 0) return 1;
        int num = Math.abs(n);
        int count = 0;
        while (num != 0) {
            count++;
            num /= 10;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(isAutomorphic(25)); // true
        System.out.println(isAutomorphic(7));  // false
    }
}

The key idea is that "ends with the number" means the last d digits of the square must match n, where d is the digit count of n. Modulo by 10^d isolates exactly those trailing digits. Using a long for the square keeps values like 99999^2 from overflowing a 32-bit int before the comparison.

Time: O(d) where d is the number of digits of nSpace: O(1)

Common Mistakes

  • Storing the square in an int, which overflows for larger inputs.
  • Comparing a fixed number of digits instead of exactly the digit count of n.

Edge Cases to Test

  • n = 0: 0 squared is 0, which ends in 0, so 0 is automorphic.
  • n = 1: 1 squared is 1, so 1 is automorphic.

Interview Follow-Ups

  • How would you list all automorphic numbers below a limit?
  • Could you avoid Math.pow by building the power of ten with a loop?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us