C# Automorphic Number Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is an Automorphic Number?

A positive integer n is automorphic when n2 ends 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
nn²Ends with n?
525Yes
636Yes
25625Yes
765776Yes
12144No

Common automorphic numbers

Handy checklist for interviews and unit tests.

List
1, 5, 6, 25, 76, 376, 625

How to Check an Automorphic Number in C#

Four clear steps — isolate the last k digits of the square.

  1. Reject n <= 0.
  2. Count digits k and build mask = 10k.
  3. Square with long: square = (long)n * n.
  4. Return square % mask == n.
MethodIdeaBest for
Modulus suffix(n * n) % Pow10(k) == nInterviews (default)
Range scanReuse 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

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.

Result
n = 76
Digits k = 2
n² = 5776
mask = 10^2 = 100
Suffix = 5776 % 100 = 76
Compare: 76 == 76
Verdict: AUTOMORPHIC

Worked Walkthrough — n = 76

The number has two digits, so use 100 to isolate the square’s final two digits.

Calculation
Square: 76 × 76 = 5776
Mask:   10² = 100
Suffix: 5776 % 100 = 76
Result: 76 == 76 → automorphic number

C# Programs

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.");
        }
    }
}

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();
    }
}

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.

Time and Space Complexity

ProgramTimeExtra space
Check one (modulus)O(k) to build 10^k (k = O(log n))O(1)
Range 1…mO(m · log m)O(1)

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.

Next: Average of N Numbers

Sum a list of values and divide by the count.

Average of N numbers tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful