A positive integer n is automorphic when n2ends with the digits of n itself.
Remember
k = number of digits in n
mask = 10^k
(n² mod mask) == n → automorphic
Example: 25 → k = 2, 25² = 625
625 % 100 = 25 → automorphic
n
n²
Ends with n?
5
25
Yes
6
36
Yes
25
625
Yes
76
5776
Yes
12
144
No
Common automorphic numbers
Handy checklist for interviews and unit tests.
List
1, 5, 6, 25, 76, 376, 625
Approach
How to Check an Automorphic Number in C#
Four clear steps — isolate the last k digits of the square.
Reject n <= 0.
Count digits k and build mask = 10k.
Square with long: square = (long)n * n.
Return square % mask == n.
Method
Idea
Best for
Modulus suffix
(n * n) % Pow10(k) == n
Interviews (default)
Range scan
Reuse IsAutomorphic from low…high
“Print all in range” follow-ups
Pseudocode
Pseudocode
function Pow10(k):
mask = 1
repeat k times:
mask = mask * 10
return mask
function IsAutomorphic(n):
if n <= 0:
return false
k = digit count of n
square = n * n // use a wide integer type
return (square mod Pow10(k)) == n
Try it
Live Preview
Enter any positive integer to see n², the suffix mask, and the verdict.
Whole numbers n ≥ 1 (capped at 99999). Tap a chip or type a value.
Start by checking one number, then reuse the same helper to find automorphic numbers in a range.
Example 1 — Check One Number
Count digits, build 10k, square with long, then compare the suffix.
C#
using System;
class Program
{
static long Pow10(int k)
{
long mask = 1;
for (int i = 0; i < k; i++)
{
mask *= 10;
}
return mask;
}
static bool IsAutomorphic(int n)
{
if (n <= 0)
{
return false;
}
int k = n.ToString().Length;
long square = (long)n * n;
return square % Pow10(k) == n;
}
static void Main()
{
int number = 76;
if (IsAutomorphic(number))
{
Console.WriteLine(number + " is an automorphic number.");
}
else
{
Console.WriteLine(number + " is not an automorphic number.");
}
}
}
Output
76 is an automorphic number.
How it works: build 10k from the number’s digit count, square in long, and compare the final k digits with n.
Example 2 — Automorphic Numbers in a Range
Reuse the same helper to list every automorphic value from 1 to 100.
C#
using System;
class Program
{
static long Pow10(int k)
{
long mask = 1;
for (int i = 0; i < k; i++)
{
mask *= 10;
}
return mask;
}
static bool IsAutomorphic(int n)
{
int k = n.ToString().Length;
long square = (long)n * n;
return square % Pow10(k) == n;
}
static void Main()
{
Console.WriteLine("Automorphic numbers from 1 to 100:");
for (int value = 1; value <= 100; value++)
{
if (IsAutomorphic(value))
{
Console.Write(value + " ");
}
}
Console.WriteLine();
}
}
Output
Automorphic numbers from 1 to 100:
1 5 6 25 76
How it works: the range loop calls IsAutomorphic for each value. From 1 through 100, the expected results are 1, 5, 6, 25, 76.
Edge Cases & Pitfalls
Check these before calling the solution done.
Wrong k
Use length of n
Mask uses digit count of n, not of n².
int overflow
Square in long
n * n in int can wrap; cast before multiplying.
Prefix?
Suffix only
Automorphic means ends with n — never a starts-with check.
1
1 is automorphic
1² = 1 ends with 1 — include it in range tests.
Analysis
Time and Space Complexity
Program
Time
Extra space
Check one (modulus)
O(k) to build 10^k (k = O(log n))
O(1)
Range 1…m
O(m · log m)
O(1)
Remember
Key Takeaways
Definition:n² ends with the digits of n.
Core test:(n*n) % 10^k == n with k = digit count.
Use long: square in a wide type before the modulus.
Test set: 25 and 76 true; 12 false; 1…100 → 1 5 6 25 76.
One line: take the last k digits of n² with modulus 10^k; if they equal n, the number is automorphic.
Frequently Asked Questions
An automorphic number is a positive integer whose square ends with the same digits as the number. For example, 25² is 625, which ends with 25.
Count the digits k, calculate 10^k, square the number, and check whether square % 10^k equals the original number.
Multiplying two int values can overflow before assignment. Casting the first value to long makes the square calculation use 64-bit arithmetic.
Yes. You can convert the square and number to strings and use EndsWith. The modulus method avoids extra strings and is usually preferred in interviews.
Building 10^k takes O(k) time, where k is the digit count. The program uses O(1) extra space.