Start with a modulus suffix check. Then list a range, and finish with a readable string ends-with variant.
Example 1 — Check One Number
Count digits, take (n * n) % pow10(k), and compare with n.
Java
class AutomorphicCheck {
static long pow10(int k) {
long p = 1L;
for (int i = 0; i < k; i++) {
p *= 10L;
}
return p;
}
static boolean isAutomorphic(int n) {
if (n <= 0) {
return false;
}
int k = 0;
for (int t = n; t > 0; t /= 10) {
k++;
}
long square = (long) n * n;
return square % pow10(k) == n;
}
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.
How It Works
Return false for non-positive inputs.
Count digits into k by dividing a copy by 10.
Square with long so n * n does not overflow int.
Isolate the last k digits with square % pow10(k).
Equality with n is the whole definition.
Example 2 — Automorphic Numbers from 1 to 50
Reuse the helper and print every match in a closed range.
Java
class AutomorphicRange {
static long pow10(int k) {
long p = 1L;
for (int i = 0; i < k; i++) {
p *= 10L;
}
return p;
}
static boolean isAutomorphic(int n) {
if (n <= 0) {
return false;
}
int k = 0;
for (int t = n; t > 0; t /= 10) {
k++;
}
long square = (long) n * n;
return square % pow10(k) == n;
}
public static void main(String[] args) {
int start = 1;
int end = 50;
System.out.println("Automorphic numbers in the range " + start + " to " + end + ":");
for (int value = start; value <= end; value++) {
if (isAutomorphic(value)) {
System.out.print(value + " ");
}
}
System.out.println();
}
}
Output
Automorphic numbers in the range 1 to 50:
1 5 6 25
How It Works
Loop from start to end inclusive.
Call isAutomorphic(value) for each value.
Print only the matches.
Within 1…50 you should see 1, 5, 6, and 25.
The helper stays pure; the loop only decides what to print.
Example 3 — String Suffix Check
Convert the square to text and ask whether it ends with the digits of n.
Java
class AutomorphicString {
static boolean isAutomorphic(int n) {
if (n <= 0) {
return false;
}
long square = (long) n * n;
String sq = Long.toString(square);
String ns = Integer.toString(n);
return sq.endsWith(ns);
}
public static void main(String[] args) {
System.out.println(isAutomorphic(25)); // true
System.out.println(isAutomorphic(12)); // false
}
}
Output
true
false
How It Works
Format n² and n as strings.
Use endsWith to compare the square’s suffix with n.
25² = 625 ends with “25” → true.
12² = 144 ends with “44” → false.
Readable, but interviews usually prefer the modulus approach.
Edge Cases & Pitfalls
Check these before calling the solution done.
Overflow
Square with long
Cast before multiplying: (long) n * n. Plain int can wrap on larger n.
Digits
Use digit count k
Compare the last k digits — not a fixed two-digit modulus unless n is always two digits.
Zero
Definitions vary
This page returns false for n <= 0. Follow the problem statement if it includes 0.
Not circular
Different from circular primes
Automorphic checks the suffix of n². Circular or cyclic ideas are unrelated.
Analysis
Time and Space Complexity
Program
Time
Extra space
Single check
O(log n)
O(1)
Range of size N
O(N log N)
O(1)
String suffix
O(log n)
O(log n) for strings
Digit count k is about log₁₀ n. Building 10^k costs O(k) multiplies.
Remember
Key Takeaways
Definition: n² ends with the same digits as n.
Classic: 25² = 625, 76² = 5776.
Check:n² % 10^k == n with a long square.
Test set: 1, 5, 6, 25, 76 yes; 12 no.
One line: if the last digit-count digits of n² equal n, it is automorphic.
Frequently Asked Questions
A positive integer n is automorphic if n squared ends with n. Example: 25² = 625, which ends with 25.
Modulus gives the last k digits directly: n² % 10^k. Compare that with n — that is the whole check.
Some books include 0. This page treats n <= 0 as not automorphic and focuses on positive integers.
Yes. Convert n² to text and check if it ends with the digits of n. Interviews usually prefer the modulus method.
If k is the digit count, one check is O(k), and k is O(log n).