Automorphic Number

Java coding interview problem for Number Logic: Automorphic Number.

Checking whether a number is an Automorphic Number is one of the most popular mathematical coding interview questions for beginners.

This problem helps interviewers evaluate your understanding of:

  • Number manipulation
  • Square calculation
  • Digit comparison
  • Loops
  • Modulus operator (%)
  • Mathematical reasoning

It is commonly asked in Java, Python, C, C++, and campus placement interviews.


What is an Automorphic Number?

An Automorphic Number is a number whose square ends with the same digits as the original number.

In simple words,

Number²

ends with

Number

Mathematical Definition

If

N

is a number,

and

Square = N × N

then

Last digits of Square

=

N

Therefore,

N is an Automorphic Number.

Example 1

Number

5

Square

5 × 5

=

25

The last digit is

5

Therefore,

5 is an Automorphic Number.

Example 2

Number

6

Square

6 × 6

=

36

The last digit is

6

Therefore,

6 is an Automorphic Number.

Example 3

Number

25

Square

25 × 25

=

625

The last two digits are

25

Therefore,

25 is an Automorphic Number.

Example 4

Number

76

Square

76 × 76

=

5776

The last two digits are

76

Therefore,

76 is an Automorphic Number.

Example 5

Number

13

Square

13 × 13

=

169

The last two digits are

69

Since

69 ≠ 13

Therefore,

13 is NOT an Automorphic Number.

Some Automorphic Numbers

Number Square Automorphic?
0 0 ✅ Yes
1 1 ✅ Yes
5 25 ✅ Yes
6 36 ✅ Yes
25 625 ✅ Yes
76 5776 ✅ Yes
13 169 ❌ No
15 225 ❌ No

Real Interview Question

Write a Java program to check whether a given number is an Automorphic Number.


Understanding the Logic

Suppose

Number = 25

Find its square.

25²

=

625

Now compare the ending digits.

625

↓

Ends with

25

Since the square ends with the original number,

25 is an Automorphic Number.

Visual Representation

Input

25

Processing

25

↓

Square

↓

625

↓

Ending Digits

↓

25

↓

Compare

↓

25 == 25

↓

Automorphic Number

Brute Force Approach

The easiest approach is:

  • Read the number.
  • Find its square.
  • Count the digits in the original number.
  • Generate a divisor (10, 100, 1000, ...).
  • Compare the last digits of the square with the original number.

Algorithm

Step 1

Read the number.

Step 2

Store the original number.

original = number;

Step 3

Find the square.

square = number * number;

Step 4

Count the digits.

Example

25

↓

2 digits

Step 5

Generate

10^digits

Example

2 digits

↓

100

Step 6

Extract the last digits.

square % divisor

Step 7

Compare

Extracted Digits == Original Number

Dry Run

Input

25

Square

625

Digits

2

Divisor

100

Extract

625 % 100

=

25

Compare

25 == 25

Output

Automorphic Number

Another Dry Run

Input

13

Square

169

Digits

2

Divisor

100

Extract

169 % 100

=

69

Comparison

69 ≠ 13

Output

Not an Automorphic Number

Approach 1 — Arithmetic Solution

This is the most common interview solution because it avoids converting numbers into strings.


Complete Java Program

public class AutomorphicNumber {

    public static void main(String[] args) {

        int number = 25;

        int original = number;

        int square = number * number;

        int digits = String.valueOf(number).length();

        int divisor = 1;

        for (int i = 0; i < digits; i++) {

            divisor *= 10;

        }

        if (square % divisor == original) {

            System.out.println(original + " is an Automorphic Number");

        } else {

            System.out.println(original + " is NOT an Automorphic Number");

        }

    }

}

Output

25 is an Automorphic Number

Step-by-Step Code Explanation

Step 1

Declare the number.

int number = 25;

Current value

25

Step 2

Store the original value.

int original = number;

This preserves the original number for comparison.


Step 3

Find the square.

int square = number * number;

Result

625

Step 4

Count the digits.

int digits = String.valueOf(number).length();

Result

2

Step 5

Generate the divisor.

int divisor = 1;

for (int i = 0; i < digits; i++) {

    divisor *= 10;

}

Result

100

Step 6

Extract the ending digits.

square % divisor

Calculation

625 % 100

=

25

Step 7

Compare

square % divisor == original

If true,

the number is an Automorphic Number.

Otherwise,

it is not.


Example Execution

Input

Number = 76

Square

5776

Ending digits

76

Output

76 is an Automorphic Number

Input

Number = 15

Square

225

Ending digits

25

Output

15 is NOT an Automorphic Number

Why Does This Work?

The algorithm computes the square of the number and determines how many digits the original number contains.

Using the modulus (%) operator with an appropriate divisor (10, 100, 1000, etc.), it extracts the same number of trailing digits from the square.

If these trailing digits match the original number, the number satisfies the definition of an Automorphic Number.


Advantages of This Approach

  • Easy to understand.
  • Uses arithmetic instead of string comparison.
  • Efficient and interview-friendly.
  • Constant extra memory.
  • Demonstrates knowledge of the modulus operator.
  • Suitable for beginner and intermediate interviews.

Drawbacks

Interviewers often ask additional questions such as:

  • Can you solve this without using String.length()?
  • Can you create a reusable isAutomorphic() method?
  • Can you print all Automorphic Numbers in a range?
  • What is the time complexity?
  • Can you solve this using string comparison?

In Part 2, we'll cover:

  • Pure Arithmetic Solution (Without String)
  • Reusable Method Approach
  • String-Based Solution
  • Printing Automorphic Numbers in a Range
  • Time and Space Complexity
  • Common Interview Mistakes
  • Interview Follow-up Questions
  • Related Coding Problems
  • Key Takeaways
  • Interview Tips

Approach 2 — Pure Arithmetic Solution (Without Using String)

Some interviewers may ask:

Can you solve this without converting the number into a String?

Yes.

Instead of using String.length(), we can count the number of digits using arithmetic.

This demonstrates a stronger understanding of number manipulation.


Java Program

public class AutomorphicNumberArithmetic {

    static boolean isAutomorphic(int number) {

        int square = number * number;

        int temp = number;

        int divisor = 1;

        while (temp > 0) {

            divisor *= 10;

            temp /= 10;

        }

        if (number == 0) {
            divisor = 10;
        }

        return square % divisor == number;

    }

    public static void main(String[] args) {

        int number = 76;

        if (isAutomorphic(number)) {

            System.out.println(number + " is an Automorphic Number");

        } else {

            System.out.println(number + " is NOT an Automorphic Number");

        }

    }

}

Output

76 is an Automorphic Number

Approach 3 — String-Based Solution

Another simple approach is to compare the ending digits using strings.

Although this approach is straightforward, interviewers generally prefer the arithmetic solution.


Java Program

public class AutomorphicNumberString {

    public static void main(String[] args) {

        int number = 25;

        int square = number * number;

        String original = String.valueOf(number);

        String result = String.valueOf(square);

        if (result.endsWith(original)) {

            System.out.println(number + " is an Automorphic Number");

        } else {

            System.out.println(number + " is NOT an Automorphic Number");

        }

    }

}

Output

25 is an Automorphic Number

Approach 4 — Print Automorphic Numbers in a Range

Interviewers sometimes ask:

Print all Automorphic Numbers between 1 and N.

We can reuse the arithmetic solution.


Java Program

public class AutomorphicNumbersRange {

    static boolean isAutomorphic(int number) {

        int square = number * number;

        int temp = number;

        int divisor = 1;

        while (temp > 0) {

            divisor *= 10;

            temp /= 10;

        }

        if (number == 0) {
            divisor = 10;
        }

        return square % divisor == number;

    }

    public static void main(String[] args) {

        int limit = 1000;

        System.out.println("Automorphic Numbers:");

        for (int i = 0; i <= limit; i++) {

            if (isAutomorphic(i)) {

                System.out.print(i + " ");

            }

        }

    }

}

Sample Output

Automorphic Numbers:

0 1 5 6 25 76 376 625

Note: The output depends on the selected range.


Dry Run

Input

Number = 76

Processing

Square

↓

5776

↓

Digits = 2

↓

Divisor = 100

↓

5776 % 100

↓

76

↓

Compare

↓

76 == 76

↓

Return true

Time Complexity

Suppose the number contains

d

digits.


Arithmetic Solution

Operation Complexity
Time O(d)
Space O(1)

String Solution

Operation Complexity
Time O(d)
Space O(d)

Printing Numbers in a Range

Operation Complexity
Time O(n × d)
Space O(1)

where

n

is the upper limit.


Comparison of Approaches

Approach Time Space Recommended
Arithmetic Solution O(d) O(1) ✅ Best for Interviews
String-Based Solution O(d) O(d) Easy to Understand
Range Solution O(n × d) O(1) Useful Follow-up

Common Mistakes

Mistake 1

Comparing the entire square.

Wrong

square == number

Correct

square % divisor == number

Mistake 2

Using the wrong divisor.

Wrong

divisor = 10;

for every number.

Correct

5

↓

10

25

↓

100

376

↓

1000

The divisor depends on the number of digits.


Mistake 3

Forgetting to count the digits.

Without knowing the number of digits, you cannot extract the correct ending digits from the square.


Mistake 4

Ignoring the number 0.

0² = 0

Therefore,

0 is also an Automorphic Number.

Mistake 5

Using string comparison when the interviewer specifically requests an arithmetic solution.

Always clarify the expected approach during interviews.


Interview Follow-up Questions

Q1. What is an Automorphic Number?

Q2. Why is 25 an Automorphic Number?

Q3. Why is 76 an Automorphic Number?

Q4. Can you solve this without using strings?

Q5. Print all Automorphic Numbers within a range.

Q6. What is the time complexity?

Q7. Can negative numbers be Automorphic Numbers?

Q8. Why do we use the modulus (%) operator?

Q9. Compare arithmetic and string approaches.

Q10. Write a reusable isAutomorphic() method.


Related Coding Problems

  • Neon Number
  • Spy Number
  • Strong Number
  • Armstrong Number
  • Palindrome Number
  • Reverse Number
  • Happy Number
  • Harshad Number

Key Takeaways

  • An Automorphic Number is a number whose square ends with the same digits as the original number.
  • Common Automorphic Numbers are:
0

1

5

6

25

76

376

625
  • Calculate the square using:
square = number * number;
  • Count the number of digits in the original number.
  • Generate the divisor (10, 100, 1000, ...).
  • Extract the trailing digits using:
square % divisor
  • Compare the extracted digits with the original number.
  • The arithmetic approach runs in O(d) time and uses O(1) extra space.

Frequently Asked Interview Questions

Q1. What is an Automorphic Number?

An Automorphic Number is a number whose square ends with the same digits as the original number.

Example

25² = 625

625 ends with 25

Therefore,

25 is an Automorphic Number.

Q2. Is 5 an Automorphic Number?

Yes.

5² = 25

The square ends with

5

Q3. Is 6 an Automorphic Number?

Yes.

6² = 36

The square ends with

6

Q4. Can negative numbers be Automorphic Numbers?

Generally, No.

Automorphic Numbers are typically defined for non-negative integers.


Q5. What is the difference between an Automorphic Number and a Neon Number?

Automorphic Number Neon Number
Square ends with the original number. Sum of the digits of the square equals the original number.
Example: 25 → 625 Example: 9 → 81 → 8 + 1 = 9

Interview Tip

If an interviewer asks:

"Write a Java program to check whether a number is an Automorphic Number."

Start with the arithmetic approach instead of the string-based solution. Explain that you first compute the square, determine the number of digits in the original number, build a divisor such as 10, 100, or 1000, and then use the modulus (%) operator to extract the trailing digits from the square. Compare these digits with the original number. Mention that this approach runs in O(d) time with O(1) extra space, where d is the number of digits. If time permits, discuss the alternative string-based solution using endsWith() and explain why the arithmetic approach is generally preferred in coding interviews.