C Automorphic Number Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is an Automorphic Number?

An automorphic number is a positive integer n whose square ends with the same digits as n.

Remember
n is automorphic  ↔  n² ends with n

Example: 76 (k = 2 digits)
  76² = 5776  → last 2 digits = 76

Common: 1, 5, 6, 25, 76
nn²Ends with n?
25625Yes
765776Yes
525Yes
12144No

How to Check an Automorphic Number in C

Count digits, then compare the last k digits of the square with n.

  1. Reject n <= 0 for this page’s positive-integer rule.
  2. Count digits into k.
  3. Compute square = (long long)n * n.
  4. Check square % 10^k == n.
MethodIdeaTimeBest for
Modulus suffixn² % 10^k == nO(log n)Interviews, default
String ends-withSquare text ends with n textO(log n)Readable variant
Range scanReuse the helperO(N log N)Listing matches

Pseudocode

Pseudocode
function IsAutomorphic(n):
    if n <= 0:
        return false

    k = digitCount(n)
    square = n * n
    return (square mod 10^k) == n

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Testn² % 10^k == n, k = digits
Square typeUse long long for n * n
1..501, 5, 6, 25
Classic25 → 625, 76 → 5776
Not12² = 144 (ends in 44)

Live Preview

Enter a positive integer to see n², the suffix, and the automorphic verdict.

Positive whole numbers. Tap a chip or type a value.

Result
n = 76, k = 2
76² = 5776
Last 2 digits = 76
Verdict: AUTOMORPHIC

Worked Walkthrough — n = 76

Square the number, then compare the last k digits with n.

Calculation
76 has 2 digits  →  k = 2

76² = 5776
5776 % 100 = 76

76 == 76  → Automorphic

C Programs

Start with a modulus suffix check. Then list a range, and finish with a readable string ends-with variant.

Example 1 — Check One Number

Count digits, take (n * n) % pow10(k), and compare with n.

C
#include <stdio.h>

long long pow10(int k) {
    long long p = 1LL;
    int i;
    for (i = 0; i < k; i++) {
        p *= 10LL;
    }
    return p;
}

int isAutomorphic(int n) {
    int k = 0;
    int t;
    long long square;

    if (n <= 0) {
        return 0;
    }

    for (t = n; t > 0; t /= 10) {
        k++;
    }

    square = (long long) n * (long long) n;
    return square % pow10(k) == n;
}

int main(void) {
    int number = 76;

    if (isAutomorphic(number)) {
        printf("%d is an automorphic number.\n", number);
    } else {
        printf("%d is not an automorphic number.\n", number);
    }

    return 0;
}

How It Works

  1. Return 0 for non-positive inputs.
  2. Count digits into k by dividing a copy by 10.
  3. Square with long long so n * n does not overflow int.
  4. Isolate the last k digits with square % pow10(k).
  5. Equality with n is the whole definition.

Example 2 — Automorphic Numbers from 1 to 50

Reuse the helper and print every match in a closed range.

C
#include <stdio.h>

long long pow10(int k) {
    long long p = 1LL;
    int i;
    for (i = 0; i < k; i++) {
        p *= 10LL;
    }
    return p;
}

int isAutomorphic(int n) {
    int k = 0;
    int t;
    long long square;

    if (n <= 0) {
        return 0;
    }

    for (t = n; t > 0; t /= 10) {
        k++;
    }

    square = (long long) n * (long long) n;
    return square % pow10(k) == n;
}

int main(void) {
    int start = 1;
    int end = 50;
    int value;

    printf("Automorphic numbers in the range %d to %d:\n", start, end);
    for (value = start; value <= end; value++) {
        if (isAutomorphic(value)) {
            printf("%d ", value);
        }
    }
    printf("\n");
    return 0;
}

How It Works

  1. Loop from start to end inclusive.
  2. Call isAutomorphic(value) for each value.
  3. Print only the matches.
  4. Within 1…50 you should see 1, 5, 6, and 25.
  5. The helper stays pure; the loop only decides what to print.

Example 3 — String Suffix Check

Convert the square to text and ask whether it ends with the digits of n.

C
#include <stdio.h>
#include <string.h>

int isAutomorphic(int n) {
    char sq[32];
    char ns[32];
    size_t sqLen;
    size_t nsLen;
    long long square;

    if (n <= 0) {
        return 0;
    }

    square = (long long) n * (long long) n;
    sprintf(sq, "%lld", square);
    sprintf(ns, "%d", n);
    sqLen = strlen(sq);
    nsLen = strlen(ns);

    if (sqLen < nsLen) {
        return 0;
    }

    return strcmp(sq + (sqLen - nsLen), ns) == 0;
}

int main(void) {
    printf("%s\n", isAutomorphic(25) ? "true" : "false");
    printf("%s\n", isAutomorphic(12) ? "true" : "false");
    return 0;
}

How It Works

  1. Format n² and n into character buffers.
  2. Compare the last nsLen characters of the square string with ns.
  3. 25² = 625 ends with “25” → true.
  4. 12² = 144 ends with “44” → false.
  5. Readable, but interviews usually prefer the modulus approach.

Edge Cases & Pitfalls

Check these before calling the solution done.

Overflow

Square with long long

Cast before multiplying: (long long)n * n. Plain int can wrap on larger n.

Digits

Use digit count k

Compare the last k digits — not a fixed two-digit modulus unless n is always two digits.

Zero

Definitions vary

This page returns false for n <= 0. Follow the problem statement if it includes 0.

Not circular

Different from circular primes

Automorphic checks the suffix of n². Circular or cyclic ideas are unrelated.

Time and Space Complexity

ProgramTimeExtra space
Single checkO(log n)O(1)
Range of size NO(N log N)O(1)
String suffixO(log n)O(log n) for buffers

Digit count k is about log₁₀ n. Building 10^k costs O(k) multiplies.

Key Takeaways

  • Definition: n² ends with the same digits as n.
  • Classic: 25² = 625, 76² = 5776.
  • Check: n² % 10^k == n with long long square.
  • Test set: 1, 5, 6, 25, 76 yes; 12 no.

One line: if the last digit-count digits of n² equal n, it is automorphic.

Frequently Asked Questions

A positive integer n is automorphic if n squared ends with n. Example: 25² = 625, which ends with 25.
Modulus gives the last k digits directly: n² % 10^k. Compare that with n — that is the whole check.
Some books include 0. This page treats n <= 0 as not automorphic and focuses on positive integers.
Yes. Convert n² to text and check if it ends with the digits of n. Interviews usually prefer the modulus method.
If k is the digit count, one check is O(k), and k is O(log n).

Next: Average of N Numbers

Find the mean of a list by summing and dividing 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.

18 people found this page helpful