JavaScript Armstrong Number Program

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

What Is an Armstrong Number?

An Armstrong number (also called a narcissistic number) equals the sum of its digits, each raised to the power of the digit count.

Remember
n is Armstrong  ↔  n = sum of (each digit ^ k)
where k = number of digits in n

Example: 153 (k = 3)
  1³ + 5³ + 3³ = 1 + 125 + 27 = 153

Also: 1–9 (one digit), 370, 371, 407
nDigit powersResult
1531³+5³+3³ = 153Armstrong
3703³+7³+0³ = 370Armstrong
1231³+2³+3³ = 36Not Armstrong
55¹ = 5Armstrong

How to Check an Armstrong Number in JavaScript

Four clear steps — count digits first, then sum powers with that same exponent.

  1. Reject n <= 0 for this page’s positive-integer rule.
  2. Count digits into k (repeatedly divide by 10).
  3. Peel each digit with % 10 and add digit^k.
  4. Compare the sum with the original number.
MethodIdeaTimeBest for
Digit loop + ipowCount k, then sum d^kO(log n)Interviews, default
Power tablePrecompute 0^k..9^kO(log n)Many checks with same k
Range scanReuse the helperO(N log N)Listing Armstrong numbers

Pseudocode

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

    k = digitCount(n)
    sum = 0
    t = n
    while t > 0:
        d = t mod 10
        sum = sum + (d ^ k)
        t = t / 10

    return sum == n

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Testsum(digit^k) == n, k = digits
Peel digitd = n % 10, then n = Math.floor(n / 10)
PowersUse integer ipow, not Math.pow
1 digit1–9 are Armstrong
3 digits153, 370, 371, 407
1..2001–9 and 153

Live Preview

Enter a positive integer to see each digit power and the Armstrong verdict.

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

Result
n = 153, k = 3
1³ + 5³ + 3³ = 1 + 125 + 27 = 153
Verdict: ARMSTRONG

JavaScript Programs

Start with a single check using integer powers. Then list a range, and finish with a digit-power lookup table. Each example has a Try it Yourself editor.

Example 1 — Check One Number

Count digits, sum ipow(d, k) for each digit, then compare with the original value.

JavaScript
function ipow(base, exp) {
  let r = 1;
  for (let e = 0; e < exp; e++) {
    r *= base;
  }
  return r;
}

function isArmstrong(number) {
  if (number <= 0) {
    return false;
  }

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

  let sum = 0;
  t = number;
  while (t > 0) {
    const d = t % 10;
    sum += ipow(d, k);
    t = Math.floor(t / 10);
  }

  return sum === number;
}

const number = 153;
if (isArmstrong(number)) {
  console.log(number + " is an Armstrong number.");
} else {
  console.log(number + " is not an Armstrong number.");
}
Try It Yourself

How It Works

  1. Return false for non-positive inputs.
  2. Count digits into k with a working copy t.
  3. Peel digits again and add ipow(d, k) each time.
  4. Compare sum with the original number.
  5. ipow stays in integer math — no floating Math.pow.

Example 2 — Armstrong Numbers from 1 to 200

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

JavaScript
function ipow(base, exp) {
  let r = 1;
  for (let e = 0; e < exp; e++) {
    r *= base;
  }
  return r;
}

function isArmstrong(num) {
  if (num <= 0) {
    return false;
  }

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

  let sum = 0;
  t = num;
  while (t > 0) {
    const d = t % 10;
    sum += ipow(d, k);
    t = Math.floor(t / 10);
  }

  return sum === num;
}

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

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

How It Works

  1. Loop from start to end inclusive.
  2. Call isArmstrong(i) for each value.
  3. Single-digit numbers print first (each is Armstrong).
  4. 153 is the only other hit in 1…200.
  5. The helper stays pure; the loop only decides what to print.

Example 3 — Digit-Power Lookup Table

Build 0^k … 9^k once, then sum with array lookups.

JavaScript
function ipow(base, exp) {
  let r = 1;
  for (let e = 0; e < exp; e++) {
    r *= base;
  }
  return r;
}

function isArmstrong(number) {
  if (number <= 0) {
    return false;
  }

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

  const pows = [];
  for (let i = 0; i < 10; i++) {
    pows[i] = ipow(i, k);
  }

  let sum = 0;
  t = number;
  while (t > 0) {
    sum += pows[t % 10];
    t = Math.floor(t / 10);
  }

  return sum === number;
}

console.log(isArmstrong(153)); // true
console.log(isArmstrong(123)); // false
Try It Yourself

How It Works

  1. Count digits into k as before.
  2. Fill pows[0..9] with i^k once.
  3. Each digit then costs an array lookup instead of a fresh power loop.
  4. 153 prints true; 123 prints false.
  5. Useful when scanning many numbers that share the same digit length.

Edge Cases & Pitfalls

Check these before calling the solution done.

Exponent

Use digit count k

Do not hard-code cubes unless the problem is only about 3-digit numbers.

Math.pow

Prefer integer powers

Floating Math.pow can round wrong. Use a small integer ipow helper.

Zero

Definitions vary

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

Division

Use Math.floor

In JavaScript, n / 10 is a float. Always use Math.floor(n / 10) when peeling digits.

Time and Space Complexity

ProgramTimeExtra space
Single checkO(log n)O(1)
Range of size NO(N log N)O(1)
Power table checkO(log n)O(1) (10 slots)

Digit count d is about log₁₀ n. Each power costs O(d) with a simple loop, still fine for interviews.

Key Takeaways

  • Definition: sum of each digit raised to the digit count equals n.
  • Classic: 153 → 1³ + 5³ + 3³ = 153.
  • Tools: count digits, peel with % 10, integer ipow.
  • Test set: 153 and 9 yes; 123 no; 1..200 → 1–9 and 153.

One line: raise each digit to the digit count and see if the sum rebuilds the number.

Frequently Asked Questions

A positive integer n is Armstrong (narcissistic) if it equals the sum of its digits, each raised to the power of the digit count. Example: 153 has 3 digits and 1³ + 5³ + 3³ = 153.
Yes. For a one-digit n, the count is 1 and n¹ = n. This page treats n <= 0 as not Armstrong.
Math.pow uses floating point and can round incorrectly for some cases. Integer exponentiation stays exact for typical interview sizes.
The same k for every digit, where k is the number of digits in n. Do not hard-code cubes unless you only care about 3-digit cases.
Counting digits and summing powers are both O(d), and d is about log₁₀ n, so one check 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: Automorphic Number

Check whether a number’s square ends with the same digits.

Automorphic number 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