Check if a Number is Automorphic | C, Python, Java Code
A number is automorphic if its square ends in the same digits: 5² = 25, 76² = 5776. Algorithm, verified examples, and programs in C, Python, and Java.
An automorphic number is one whose square ends in the same digits as the number itself: 5² = 25, 6² = 36, 25² = 625, and 76² = 5776.
This type of check appears in placement test coding rounds alongside similar number-theory problems such as perfect number verification and digit sum programs. The algorithm is short and runs in O(log N) time. Two implementation styles are worth knowing: a digit-comparison loop (C-style) and a string suffix check (Python and Java).
What Is an Automorphic Number?
A number N is automorphic if the last d digits of N² equal N, where d is the digit count of N.
According to the Wikipedia article on automorphic numbers, these are sometimes called circular numbers in older references. The term automorphic is standard in Indian placement prep material and in most competitive programming resources.
Verified examples (re-derived from first principles):
- 5 (1 digit): 5² = 25. Last 1 digit of 25 = 5. Automorphic.
- 6 (1 digit): 6² = 36. Last 1 digit of 36 = 6. Automorphic.
- 25 (2 digits): 25² = 625. Last 2 digits of 625 = 25. Automorphic.
- 76 (2 digits): 76² = 5776. Last 2 digits of 5776 = 76. Automorphic.
- 376 (3 digits): 376² = 141376. Last 3 digits = 376. Automorphic.
- 625 (3 digits): 625² = 390625. Last 3 digits = 625. Automorphic.
The OEIS sequence A003226 lists all automorphic numbers in base 10: 0, 1, 5, 6, 25, 76, 376, 625, 9376, 90625, …
Two non-examples:
- 7: 7² = 49. Last digit = 9, not 7. Not automorphic.
- 12: 12² = 144. Last 2 digits = 44, not 12. Not automorphic.
The single-digit automorphic numbers are exactly 0, 1, 5, and 6. Every other single-digit number fails the check.
Algorithm to Check If a Number Is Automorphic
Two methods work. Both produce the same result; they differ only in how the digit comparison is expressed.
Method 1: Digit-by-digit comparison
This is the standard approach shown in placement test explanations:
- Step 1: Compute
square = N * N. - Step 2: Keep a copy of N in
temp. - Step 3: While
temp > 0, extract the last digit of each:square % 10andtemp % 10. If they differ, N is not automorphic. Return false. - Step 4: Shift both right:
square /= 10andtemp /= 10. - Step 5: If the loop completes without a mismatch, N is automorphic. Return true.
The loop runs exactly d iterations (d = number of digits in N). It short-circuits on the first mismatch, so the worst case is d iterations and the best case is 1.
Method 2: Power-of-10 modulus
- Step 1: Count d, the digit count of N.
- Step 2: Compute
square = N * N. - Step 3: Compute
mod = 10^d. - Step 4: If
square % mod == N, the number is automorphic.
Method 2 is a single comparison instead of a loop. Both run in O(log N) time because counting digits or looping through them both take d steps.
Program in C
#include <stdio.h>
/* Returns 1 if num is automorphic, 0 otherwise. */
int isAutomorphic(long long num) {
long long square = num * num;
long long temp = num;
if (num == 0) return 1; /* 0 squared = 0, ends in 0 */
while (temp > 0) {
if (square % 10 != temp % 10) return 0;
square /= 10;
temp /= 10;
}
return 1;
}
int main(void) {
long long num;
printf("Enter a number: ");
scanf("%lld", &num);
if (isAutomorphic(num))
printf("Automorphic Number\n");
else
printf("Not an Automorphic Number\n");
return 0;
}
Key points in the C implementation:
long longstores the square safely for inputs up to several thousand digits. Using plainintsilently overflows for inputs above about 46,340.square % 10extracts the last digit of the square.square /= 10removes it.temp % 10extracts the corresponding last digit of the original number.- The explicit guard
if (num == 0) return 1handles the edge case before the while loop, which would otherwise not execute for input 0.
Program in Python
Python’s str.endswith() expresses the same check in one line:
def is_automorphic(num):
square = num * num
return str(square).endswith(str(num))
# Test the function against known cases
test_values = [0, 1, 5, 6, 25, 76, 376, 625, 7, 12, 10]
for n in test_values:
result = "Automorphic" if is_automorphic(n) else "Not Automorphic"
print(f"{n}: {result}")
Python integers have no fixed overflow limit, so this version handles large automorphic numbers such as 9376 and 90625 without modification. The string conversion approach is also immune to the off-by-one errors that can affect the digit-loop in C when counting digits of edge-case inputs.
Program in Java
public class AutomorphicNumber {
static boolean isAutomorphic(int num) {
long square = (long) num * num; // cast prevents int overflow
String numStr = Integer.toString(num);
String squareStr = Long.toString(square);
return squareStr.endsWith(numStr);
}
public static void main(String[] args) {
int[] testValues = {0, 1, 5, 6, 25, 76, 376, 625, 7, 12, 10};
for (int n : testValues) {
String result = isAutomorphic(n) ? "Automorphic" : "Not Automorphic";
System.out.println(n + ": " + result);
}
}
}
The (long) num * num cast is necessary. Without it, Java evaluates num * num as 32-bit integer arithmetic, which overflows silently for large inputs. Casting one operand to long before the multiplication forces 64-bit arithmetic and avoids the issue.
Time and Space Complexity
Both methods run in O(log N) time. The digit-comparison loop (Method 1) runs exactly d iterations, where d is the digit count of N. For 376, d = 3 and the loop runs 3 times. Method 2 spends the same number of steps counting digits to compute 10^d.
Space complexity is O(1) for the digit-loop methods (C implementation): only a constant number of integer variables regardless of input size. The string-based approaches in Python and Java allocate O(d) space for the string representations of the number and its square, but d is at most the number of decimal digits in N, which is small in practice.
For placement round answers, state: “O(log N) time, O(1) space for the digit-loop approach.”
Test Cases and Edge Cases
| Input | N² | Expected Output |
|---|---|---|
| 0 | 0 | Automorphic Number |
| 1 | 1 | Automorphic Number |
| 5 | 25 | Automorphic Number |
| 6 | 36 | Automorphic Number |
| 25 | 625 | Automorphic Number |
| 76 | 5776 | Automorphic Number |
| 376 | 141376 | Automorphic Number |
| 7 | 49 | Not an Automorphic Number |
| 12 | 144 | Not an Automorphic Number |
| 10 | 100 | Not an Automorphic Number |
Three edge cases worth testing explicitly:
- N = 0: 0² = 0. The digit-loop’s
while (temp > 0)condition is false from the start, so the C code needs the explicit guardif (num == 0) return 1shown above. - Numbers ending in zero (10, 100): 10² = 100. The last 2 digits are 00, not 10, so 10 is not automorphic. The digit loop handles this correctly without any special case.
- Large inputs: Use
long longin C orlongin Java. The digit 9376 has square 87,909,376, withinlong longrange but not withinintrange for the multiplication step.
The automorphic check is a clean entry point into the digit-manipulation family in placement rounds. The num % 10 and num /= 10 pattern from the C code also drives the digit sum program, palindrome checks, and Armstrong number detection. Once the loop skeleton is clear, the pattern transfers to any of these checks directly. If you want to extend that digit logic into AI-assisted tools that parse numeric sequences from unstructured text, TinkerLLM at ₹299 is a sandbox for exactly that kind of applied programming.
Primary sources
Frequently asked questions
What is an automorphic number?
A number is automorphic if its square ends in the same digits as the number itself. For example, 5² = 25 ends in 5, and 76² = 5776 ends in 76. Both are automorphic.
Is 0 an automorphic number?
Yes. 0² = 0, which ends in 0. The digit-loop implementation handles this with an explicit guard before the while loop. 0 is automorphic by definition.
Is 1 an automorphic number?
Yes. 1² = 1, which ends in 1. The four single-digit automorphic numbers are 0, 1, 5, and 6.
What are all single-digit automorphic numbers?
The four single-digit automorphic numbers are 0 (0²=0), 1 (1²=1), 5 (5²=25), and 6 (6²=36). The digits 2, 3, 4, 7, 8, and 9 are not automorphic.
What is the time complexity of the automorphic number check?
O(log N), because the digit-comparison loop runs exactly once per digit of N. For a 3-digit number like 376, the loop runs 3 iterations. For a 4-digit number like 9376, it runs 4 iterations.
What happens when you check a number like 10 that ends in zero?
10 is not automorphic. 10² = 100. Tracing the loop: first iteration compares 100%10=0 with 10%10=0 (both zero, a match); second iteration compares 10%10=0 with 1%10=1 (mismatch). The function returns false on the second step.
What is the difference between automorphic and Armstrong numbers?
An Armstrong number equals the sum of each digit raised to the power of the digit count: 153 = 1³ + 5³ + 3³. An automorphic number is one whose square ends in the same digits. Different definitions, different sets of numbers.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)