An automorphic number in Java is a number whose square ends with the same digits as the number itself. For example, 5² = 25 (ends in 5), 25² = 625 (ends in 25), and 376² = 141376 (ends in 376). In Java, you can check whether a number is automorphic by squaring it, counting its digits, and using the modulus operator to extract the last N digits of the square for comparison.
This article walks you through multiple Java programs to check automorphic numbers, using the modulus operator, string manipulation, while loop, and for loop, with step-by-step explanations and output examples. You will also learn how to print all automorphic numbers within a range, understand time and space complexity, and avoid common mistakes beginners make.
Whether you are preparing for competitive programming, a Java interview, or just brushing up on number theory patterns, mastering automorphic numbers in Java strengthens your programming logic. It is one of many problems covered in this complete guide to Number Programs in Java that help beginners practice core programming concepts.
What is an Automorphic Number?
An automorphic number is a special number in mathematics where the square of the number has the same ending digits as the number itself. In other words, if you square the number and the last digits of the result match the original number exactly, the number is automorphic.
Formal definition: A number N is automorphic if (N² mod 10ᵉ) = N, where d is the number of digits in N.
Examples of Automorphic Numbers
| Number | Square | Last Digits Match? | Automorphic? |
| 5 | 25 | 5 | Yes |
| 6 | 36 | 6 | Yes |
| 25 | 625 | 25 | Yes |
| 76 | 5776 | 76 | Yes |
| 376 | 141376 | 376 | Yes |
| 625 | 390625 | 625 | Yes |
| 9376 | 87909376 | 9376 | Yes |
| 7 | 49 | 9 (not 7) | No |
| 13 | 169 | 69 (not 13) | No |
How to Check if a Number is Automorphic in Java

Before writing any code, it helps to understand the underlying logic clearly. Here is the step-by-step approach:
- Compute the square of the given number.
- Count the number of digits in the original number.
- Extract the last N digits from the square using the modulus operator: square % (10^N).
- Compare the extracted digits with the original number. If they match, the number is automorphic.
Worked Example — Is 25 Automorphic?
- Number = 25
- Square = 25 x 25 = 625
- Number of digits in 25 = 2
- Last 2 digits of 625 = 625 % 100 = 25
- 25 == 25 ✓ Yes, 25 is automorphic
Java Program to Check Automorphic Number Using Modulus Operator
This is the most widely used and efficient approach. It uses Math.pow() to compute 10^d and the modulus operator to extract the tail digits of the square.
Algorithm
- Read the input number.
- Compute square = number * number.
- Count digits d in the number using a while loop.
- Compute lastDigits = square % (int)Math.pow(10, d).
- If lastDigits == number, print automorphic; else print not automorphic.
Code
import java.util.Scanner;
public class AutomorphicModulus {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = in.nextInt();
int numCopy = number;
int square = number * number;
int d = 0;
// Count digits in the number
while (number > 0) {
d++;
number /= 10;
}
// Extract last d digits from the square
int lastDigits = (int)(square % Math.pow(10, d));
if (lastDigits == numCopy)
System.out.println(numCopy + " is an Automorphic number.");
else
System.out.println(numCopy + " is NOT an Automorphic number.");
}
}
Output
Enter a number: 25
25 is an Automorphic number.
Enter a number: 13
13 is NOT an Automorphic number.
How the Code Works
- The while loop counts digits by repeatedly dividing by 10 until the number becomes 0.
- Math.pow(10, d) gives 10 raised to the digit count, e.g., 100 for a 2-digit number.
- The modulus extracts the rightmost d digits from the square.
- A simple equality check determines if the number is automorphic.
Java Program to Check Automorphic Number Using String Manipulation
This approach converts both the number and its square to strings, then uses Java’s built-in endsWith() method. It is easy to read and avoids manual digit counting.
Code
import java.util.Scanner;
public class AutomorphicString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int square = number * number;
String numStr = String.valueOf(number);
String squareStr = String.valueOf(square);
if (squareStr.endsWith(numStr))
System.out.println(number + " is an Automorphic number.");
else
System.out.println(number + " is NOT an Automorphic number.");
scanner.close();
}
}
Output
Enter a number: 76
76 is an Automorphic number.
Why Use This Approach?
- No need to manually count digits.
- endsWith() handles all edge cases cleanly.
- Best for beginners due to its readability.
Java Program to Check Automorphic Number Using While Loop
This approach compares digits one by one from the right using a while loop. It iterates while the temp copy of the number is greater than zero, comparing last digits at each step.
Code
import java.util.Scanner;
public class AutomorphicWhile {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int square = number * number;
int temp = number;
boolean isAutomorphic = true;
while (temp > 0) {
if (temp % 10 != square % 10) {
isAutomorphic = false;
break;
}
temp /= 10;
square /= 10;
}
if (isAutomorphic)
System.out.println(number + " is an Automorphic number.");
else
System.out.println(number + " is NOT an Automorphic number.");
scanner.close();
}
}
Output
Enter a number: 625
625 is an Automorphic number.Java Program to Check Automorphic Number Using For Loop
Functionally identical to the while loop approach, but rewritten as a for loop for those who prefer compact loop syntax.
Code
import java.util.Scanner;
public class AutomorphicFor {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int square = number * number;
boolean isAutomorphic = true;
for (int temp = number; temp > 0; temp /= 10, square /= 10) {
if (temp % 10 != square % 10) {
isAutomorphic = false;
break;
}
}
if (isAutomorphic)
System.out.println(number + " is an Automorphic number.");
else
System.out.println(number + " is NOT an Automorphic number.");
scanner.close();
}
}
Output
Enter a number: 376
376 is an Automorphic number.Java Program to Print All Automorphic Numbers in a Range (0–10,000)
This program iterates through every number from 0 to 10,000 and prints all automorphic numbers using a reusable helper method.
Code
public class AutomorphicRange {
static boolean isAutomorphic(int number) {
if (number == 0) return true;
int square = number * number;
int temp = number;
while (temp > 0) {
if (temp % 10 != square % 10) return false;
temp /= 10;
square /= 10;
}
return true;
}
public static void main(String[] args) {
System.out.println("Automorphic numbers between 0 and 10000:");
for (int i = 0; i <= 10000; i++) {
if (isAutomorphic(i))
System.out.print(i + " ");
}
}
}
Output
Automorphic numbers between 0 and 10000:
0 1 5 6 25 76 376 625 9376Notice the pattern: automorphic numbers are quite rare. Between 0 and 10,000, there are only nine of them. This rarity is part of what makes them mathematically interesting.
Time and Space Complexity
| Approach | Time Complexity | Space Complexity | Notes |
| Modulus Operator | O(log₁₀N) | O(1) | Best for performance |
| String Manipulation | O(d) | O(d) | d = digit count; clean and readable |
| While Loop (digit compare) | O(log₁₀N) | O(1) | Equivalent to modulus approach |
| For Loop (digit compare) | O(log₁₀N) | O(1) | Same as while loop |
| Range check (0 to 10000) | O(N × log₁₀N) | O(1) | N = upper bound |
Why O(log₁₀N)? The number of digits in N is proportional to log₁₀(N). For each digit, the loop performs a constant amount of work, so the total work is proportional to the digit count.
Why O(1) space? None of these programs allocate memory that grows with the input. They only use a fixed number of integer variables regardless of how large N is.
Automorphic vs Cyclic vs Narcissistic Numbers
These three number types are often confused. Here is a quick comparison to set the record straight:
| Property | Automorphic Number | Narcissistic Number | Cyclic Number |
| Definition | Square ends with same digits as N | Sum of digits each raised to power of digit count equals N | Multiples are cyclic permutations of its digits |
| Example | 25 (25² = 625) | 153 (1³+5³+3³=153) | 142857 (x1 to x6 are rotations) |
| Key operation | Modulus / endsWith() | Power & sum | Multiplication check |
| Frequency | Very rare | Rare | Extremely rare |
| Java technique | %, Math.pow() | Math.pow(), loop | String rotation / multiplication |
Common Errors and Debugging Tips

1. Integer Overflow for Large Numbers
Squaring a large int can overflow. For example, 100000 * 100000 = 10,000,000,000 which exceeds Integer.MAX_VALUE (2,147,483,647). Always use long when the input could be large.
long square = (long) number * number; // Safe for large inputs
2. Not Handling Edge Cases: 0 and 1
Both 0 and 1 are automorphic (0² = 0, 1² = 1). Make sure your program handles them. The digit-counting while loop must handle number = 0 as a special case since the loop body never executes.
3. Off-By-One in Digit Counting
When using Math.log10(number) + 1 to count digits, this fails for number = 0 (log10(0) is undefined). It is safer to count digits using repeated division or String.valueOf(number).length().
4. Incorrect Digit Extraction
Remember: to extract the last d digits from a number, compute number % (10^d), not number % d. Using the wrong divisor is a very common bug.
5. Negative Number Handling
Negative numbers do not work directly. Always take Math.abs(number) before squaring and comparing if your program must accept negative inputs.
Frequently Asked Questions (FAQs)
Q1. What is an automorphic number in Java?
An automorphic number in Java is a number whose square ends with the same digits as the number itself. For example, 6² = 36 (ends in 6), so 6 is automorphic. You check this in Java by squaring the number, counting its digits, and using the modulus operator to compare.
Q2. Is 1 an automorphic number?
Yes. 1² = 1, and the last digit of 1 is 1. So 1 is an automorphic number.
Q3. Is 0 an automorphic number?
Yes. 0² = 0. The number 0 satisfies the automorphic property and is typically included in lists of automorphic numbers.
Q4. What is the difference between automorphic and narcissistic numbers?
An automorphic number checks the ending digits of its square. A narcissistic (Armstrong) number checks whether the sum of its digits each raised to the power of the digit count equals the number itself. They are completely different properties.
Q5. How do you find all automorphic numbers in a range in Java?
Write a helper method isAutomorphic(int n) that returns true if n is automorphic, then loop from your lower bound to the upper bound and print every number for which isAutomorphic() returns true. See Section 8 of this article for the full code.
Q6. Why should I learn about automorphic numbers?
Automorphic number problems are excellent practice for modulus operations, loop design, and digit manipulation in Java. They appear in ICSE/ISC board exams, coding contests, and Java developer interviews as exercises in logical thinking.
Q7. Can I use String methods instead of math to check automorphic numbers?
Yes. Convert both the number and its square to strings and use squareStr.endsWith(numStr). This is clean and avoids manual digit counting. See Section 5 for the full code.
Conclusion
In this article, you learned everything about automorphic numbers in Java. Here is a quick recap:
- An automorphic number is one whose square ends with the same digits.
- You can check it using the modulus operator, string manipulation, while loop, or for loop.
- All digit-comparison approaches run in O(log₁₀N) time and O(1) space.
- Between 0 and 10,000, there are only nine automorphic numbers: 0, 1, 5, 6, 25, 76, 376, 625, 9376.
- Use long instead of int to avoid overflow with large inputs.
Automorphic numbers are a great problem to practice modular arithmetic, string operations, and loop design in Java. They appear regularly in coding interviews, school exams, and competitive programming. Mastering this concept also builds the foundational intuition needed for harder number-theory problems down the line.









