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)
String EndsWith
(n*n).ToString().EndsWith(n.ToString())
Quick demos
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
Cheat sheet
Goal
Pattern
Digit count
n.ToString().Length
Build 10k
Loop multiply by 10, or Math.Pow(10, k) cast carefully
Safe square
long square = (long)n * n;
Automorphic test
square % mask == 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.
Digit count k = 2, so the mask is 100. Take the last two digits of the square.
Step
Value
Square
76 × 76 = 5776
Mask
10² = 100
Suffix
5776 % 100 = 76
Compare
76 == 76 → automorphic
Same idea for 25: 625 % 100 = 25. Counter-example 12: 144 % 100 = 44 ≠ 12.
Code
C# Programs
Three programs: modulus check, user input with square printed, and a range list. Use View Output for sample results.
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
1. Guard and count. Reject non-positive inputs. k is the digit length of n — not the length of the square.
2. Wide square.(long)n * n avoids int overflow on larger values.
3. Suffix compare.square % Pow10(k) keeps the last k digits. For 76: 5776 % 100 = 76.
Example 2 — User Input + Square
Interview follow-up: read n safely and print n², the mask, and 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 void Main()
{
Console.Write("Enter a positive integer: ");
if (!int.TryParse(Console.ReadLine(), out int n) || n < 1)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
int k = n.ToString().Length;
long mask = Pow10(k);
long square = (long)n * n;
long suffix = square % mask;
Console.WriteLine("n² = " + square);
Console.WriteLine("mask = 10^" + k + " = " + mask);
Console.WriteLine("suffix = " + suffix);
if (suffix == n)
{
Console.WriteLine(n + " is an automorphic number.");
}
else
{
Console.WriteLine(n + " is not an automorphic number.");
}
}
}
Output (when user enters 25)
Enter a positive integer: 25
n² = 625
mask = 10^2 = 100
suffix = 25
25 is an automorphic number.
How It Works
1. Safe input.int.TryParse avoids FormatException on bad text.
2. Show the parts. Printing square, mask, and suffix makes wrong answers easy to debug.
3. Same rule. Automorphic means suffix == n. Try 12: suffix is 44, so not automorphic.
Example 3 — 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
1. One helper. The range loop only calls IsAutomorphic — no duplicated suffix logic.
2. Expected hits. In 1…100 the list is 1 5 6 25 76 — a classic interview checkpoint.
3. Alternate note. You can also write ((long)n * n).ToString().EndsWith(n.ToString()) for the same verdict; prefer modulus in interviews.
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.
n <= 0
Reject early
Return false for non-positive inputs in this tutorial’s convention.
1
1 is automorphic
1² = 1 ends with 1 — include it in range tests.
Circular
Different concept
Do not confuse with circular/cyclic numbers.
Analysis
Time and Space Complexity
Program
Time
Extra space
Check one (modulus)
O(k) to build 10^k (k = O(log n))
O(1)
Check one (EndsWith)
O(k) string work
O(k) for the strings
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
A positive integer n is automorphic when n squared ends with the digits of n. Example: 25² = 625 ends with 25.
If n has k digits, compute (n * n) % (10^k) and compare with n. Equality means automorphic.
n * n can overflow a 32-bit int. Cast to long (or use checked long math) before squaring.
Some definitions include 0. This tutorial checks positive integers only, so n <= 0 returns false.
Yes: (n*n).ToString().EndsWith(n.ToString()). Interviews usually prefer the modulus form.
No. Automorphic is a square-suffix property. Circular/cyclic numbers are a different idea.
1, 5, 6, 25, 76, 376, and 625 are frequent base-10 examples.
Count digits k, build 10^k, then return (n*n) % 10^k == n. Mention EndsWith as an alternate.
Building 10^k is O(k) with k = O(log n). The check itself is O(1) arithmetic after that. Extra space is O(1).
Loop from low to high and reuse the same IsAutomorphic helper.
🤔
Did you know?
In base 10, classic automorphic values include 1, 5, 6, 25, and 76. Longer ones include 376 and 625.