C# Automorphic Number Program

Beginner
6 min read
Updated: Sep 2026
3 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)
String EndsWith(n*n).ToString().EndsWith(n.ToString())Quick demos
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

Cheat sheet

GoalPattern
Digit countn.ToString().Length
Build 10kLoop multiply by 10, or Math.Pow(10, k) cast carefully
Safe squarelong square = (long)n * n;
Automorphic testsquare % mask == 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

Digit count k = 2, so the mask is 100. Take the last two digits of the square.

StepValue
Square76 × 76 = 5776
Mask10² = 100
Suffix5776 % 100 = 76
Compare76 == 76 → automorphic

Same idea for 25: 625 % 100 = 25. Counter-example 12: 144 % 100 = 44 ≠ 12.

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

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

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

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.

Time and Space Complexity

ProgramTimeExtra space
Check one (modulus)O(k) to build 10^k (k = O(log n))O(1)
Check one (EndsWith)O(k) string workO(k) for the strings
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

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.

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