JavaScript 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 JavaScript

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 = n * n.
  4. Check square % 10^k == n.
MethodIdeaTimeBest for
Modulus suffixn² % 10^k == nO(log n)Interviews, default
String ends-withString(n²).endsWith(String(n))O(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
Large nStay within safe integers, or use BigInt
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

JavaScript Programs

Start with a modulus suffix check. Then list a range, and finish with a readable string ends-with variant. Each example has a Try it Yourself editor.

Example 1 — Check One Number

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

JavaScript
function pow10(k) {
  let p = 1;
  for (let i = 0; i < k; i++) {
    p *= 10;
  }
  return p;
}

function isAutomorphic(n) {
  if (n <= 0) {
    return false;
  }

  let k = 0;
  for (let t = n; t > 0; t = Math.floor(t / 10)) {
    k++;
  }

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

const number = 76;
if (isAutomorphic(number)) {
  console.log(number + " is an automorphic number.");
} else {
  console.log(number + " is not an automorphic number.");
}
Try It Yourself

How It Works

  1. Return false for non-positive inputs.
  2. Count digits into k by dividing a copy by 10.
  3. Square with n * n (safe for typical interview sizes).
  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.

JavaScript
function pow10(k) {
  let p = 1;
  for (let i = 0; i < k; i++) {
    p *= 10;
  }
  return p;
}

function isAutomorphic(n) {
  if (n <= 0) {
    return false;
  }

  let k = 0;
  for (let t = n; t > 0; t = Math.floor(t / 10)) {
    k++;
  }

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

const start = 1;
const end = 50;
console.log("Automorphic numbers in the range " + start + " to " + end + ":");

let line = "";
for (let value = start; value <= end; value++) {
  if (isAutomorphic(value)) {
    line += value + " ";
  }
}
console.log(line.trim());
Try It Yourself

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.

JavaScript
function isAutomorphic(n) {
  if (n <= 0) {
    return false;
  }

  const square = String(n * n);
  const ns = String(n);
  return square.endsWith(ns);
}

console.log(isAutomorphic(25)); // true
console.log(isAutomorphic(12)); // false
Try It Yourself

How It Works

  1. Convert n² and n to strings.
  2. endsWith checks whether the square text finishes with the digits of n.
  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.

Large n

Watch Number precision

For very large n, n * n may lose precision. Use BigInt when values leave the safe-integer range.

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.

Division

Use Math.floor

When counting digits, use Math.floor(t / 10) — plain / is floating point in JavaScript.

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 strings

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 (or endsWith).
  • 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 (String.endsWith). Interviews usually prefer the modulus method.
If k is the digit count, one check is O(k), and k is O(log n).
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit the input and Run.

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.

14 people found this page helpful