Definition
Sum = n
n is Armstrong when each digit raised to power k (digit count) adds up to n.
An Armstrong number equals the sum of its digits each raised to the power of the digit count. This tutorial covers the definition, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
Sum = n
n is Armstrong when each digit raised to power k (digit count) adds up to n.
Find k
Use String(n).length or a loop dividing by 10 — k is the exponent for every digit.
% 10 // 10
Extract digits with modulo, add intPow(digit, k), then compare the total to n.
1³+5³+3³
The schoolbook example: 1³ + 5³ + 3³ = 153 — your golden test.
Try any n
Type a number and see each digit-power term plus the final verdict.
Complexity
One check walks each digit once; extra space stays O(1).
An Armstrong number (also called a narcissistic number) is a positive integer that equals the sum of its digits each raised to the power of how many digits it has.
For a number n with k digits, compute d1k + d2k + … + dkk. If that sum is n, the number is Armstrong. The classic classroom example is 153: 1³ + 5³ + 3³ = 153.
It trains digit extraction, counting, and integer powers — three skills that show up constantly in interview number problems.
Every digit uses power k — the digit count of n.
Single-digit numbers satisfy d¹ = d, so they are Armstrong.
Extract digits from a temp copy so you can still compare to n.
Use intPow (or careful integer loops) — avoid Math.pow rounding traps.
In short: count digits k, sum each digitk, and check whether that sum equals the original number.
Given a positive integer n, decide whether it is an Armstrong number.
// Example: n = 153 (k = 3 digits)
// 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
// sum === n → Armstrong | Item | Type | Description |
|---|---|---|
n | number | Positive integer to test (this tutorial returns false for n <= 0). |
| Return / print | bool / text | true / message when the digit-power sum equals n. |
function isArmstrong(n):
if n <= 0:
return false
k = number of digits in n
sum = 0
for each digit d in n:
sum = sum + d^k
return sum == n | Method | Idea | Notes |
|---|---|---|
| Arithmetic loop | % 10 / Math.floor(temp / 10) with intPow(digit, k) | Interview classic; O(1) extra space |
| String digits | Iterate characters of String(n) | Very readable; same O(log n) time |
| Goal | Pattern |
|---|---|
| Digit count k | power = String(n).length |
| Next digit | digit = temp % 10 |
| Drop last digit | temp = Math.floor(temp / 10) |
| Add powered digit | total += intPow(digit, power) |
| Armstrong test | total === n |
| 3-digit classics | 153, 370, 371, 407 |
All can detect Armstrong numbers — generality differs.
% 10 // 10Best default for interviews; no string conversion
for ch in String(n)Short and clear; fine when readability wins
fixed cubeOnly correct for 3-digit numbers — avoid as general solution
use k digitsAlways set the exponent from the digit count
Reach for Armstrong drills when digit loops and powers matter.
Quick check of modulo loops, exponents, and equality returns.
Classic first program after learning loops and %.
“Print all Armstrong numbers from 1 to N” reuses one helper.
Makes % 10 and // 10 feel concrete with a famous example.
Very large k makes powers enormous — discuss constraints in the prompt.
Key benefit: one short problem that covers digits, powers, helpers, and O(log n) reasoning.
Enter a positive integer to see each digit-power term and the verdict.
Three complete JavaScript programs with Try it Yourself editors — check one number, print a range, and a string-based variant. Click View Output to reveal sample console results.
Arithmetic digit extraction — the interview default.
Count digits, sum each digit raised with intPow, then compare with the original value. Prefer intPow over Math.pow for exact equality.
function intPow(base, exp) {
let result = 1;
for (let e = 0; e < exp; e++) {
result *= base;
}
return result;
}
function isArmstrong(n) {
if (n <= 0) {
return false;
}
const power = String(n).length;
let total = 0;
let temp = n;
while (temp > 0) {
const digit = temp % 10;
total += intPow(digit, power);
temp = Math.floor(temp / 10);
}
return total === n;
}
const number = 153;
if (isArmstrong(number)) {
console.log(number + " is an Armstrong number.");
} else {
console.log(number + " is not an Armstrong number.");
} Guard non-positive inputs, compute power once, then walk digits via temp so n stays intact for the final comparison.
Reuse the helper across a closed range.
Loop from start to end and print every value that passes the check.
function intPow(base, exp) {
let result = 1;
for (let e = 0; e < exp; e++) {
result *= base;
}
return result;
}
function isArmstrong(n) {
if (n <= 0) {
return false;
}
const power = String(n).length;
let total = 0;
let temp = n;
while (temp > 0) {
const digit = temp % 10;
total += intPow(digit, power);
temp = Math.floor(temp / 10);
}
return total === n;
}
const start = 1;
const end = 200;
const hits = [];
for (let value = start; value <= end; value++) {
if (isArmstrong(value)) {
hits.push(value);
}
}
console.log("Armstrong numbers in the range " + start + " to " + end + ":");
console.log(hits.join(" ")); Single-digit values appear first (each is Armstrong), then 153 is the only other hit in 1…200. The helper stays pure; the loop only decides what to collect and print.
Same math with string iteration.
Convert to a string, raise each character digit to power s.length with intPow, and compare.
function intPow(base, exp) {
let result = 1;
for (let e = 0; e < exp; e++) {
result *= base;
}
return result;
}
function isArmstrong(n) {
if (n <= 0) {
return false;
}
const s = String(n);
const power = s.length;
let total = 0;
for (let i = 0; i < s.length; i++) {
total += intPow(Number(s[i]), power);
}
return total === n;
}
console.log(isArmstrong(153));
console.log(isArmstrong(123)); String(n) makes digit count and iteration obvious. Prefer this when readability matters; prefer arithmetic peel when you want to avoid string conversion.
If n <= 0, return false for this tutorial’s positive-integer definition.
Set k (power) from the number of digits in n.
Extract each digit and add intPow(digit, k) into a running total.
Return true only when the powered digit sum equals the original number.
n = 153Trace the arithmetic method. Digit count k = 3. Start with temp = 153 and total = 0.
temp | Digit | Add | total |
|---|---|---|---|
153 | 3 | 3**3 = 27 | 27 |
15 | 5 | 5**3 = 125 | 152 |
1 | 1 | 1**3 = 1 | 153 |
Final check: 153 == 153 → Armstrong.
Where Armstrong checks show up beyond the interview prompt.
Standard warm-up for digit loops and powers.
Example: write is_armstrong(n).
Makes % 10 and // 10 memorable with 153.
Example: chalkboard digit peel.
Print or count Armstrong numbers inside bounds.
Example: all hits from 1 to 1000.
Skills transfer to Armstrong-like and digit-sum variants.
Example: Disarium / automorphic follow-ups.
Argue O(log n) from digit count convincingly.
Example: “how many loop iterations?”
Shows why exact integer powers matter for equality.
Example: reject math.pow floats.
Pro Tip: keep one is_armstrong helper and reuse it for single checks and range printers — less duplicated digit logic.
Why this pattern works well in interviews and classwork.
Count digits, sum powers, compare — almost no translation gap.
Using k from the digit count handles 1-digit through multi-digit cases.
A few integers suffice — O(1) extra space.
153 / 370 / 371 / 407 and 123 give instant confidence.
Pro Tip: say “exponent equals digit count” out loud before coding — it stops the fixed-cube mistake.
Small habits that keep Armstrong code interview-ready.
Use temp = n so the original value survives for comparison.
Count digits before the sum loop — do not recalculate k each iteration.
intPowStay exact; floating powers can spoil equality on larger inputs.
Assert true on 153/370 and false on 123 before moving on.
Ask whether 0 counts; this page treats only positive integers.
Pro Tip: dry-run 153 on paper once — it catches off-by-one digit-count bugs faster than guessing.
Mistakes that commonly break Armstrong solutions.
Hard-coding ^3 fails for 1-digit and multi-digit cases beyond 3.
→ Set the exponent from the digit count every time.
Looping on n itself leaves nothing to compare against.
→ Peel digits from a temp copy.
math.pow can introduce rounding that breaks equality.
→ Prefer integer intPow.
Some students assume only 3-digit examples count.
→ Remember 1–9 are Armstrong under the standard definition.
Negative or zero values need an explicit policy.
→ Return false early for n <= 0 in this tutorial.
Check these inputs before calling the solution done.
This tutorial uses positive integers only.
Single-digit values satisfy d¹ = d.
Must return true for any correct implementation.
Sum is 36 — must return false.
Extract digits from temp, compare against n.
JavaScript ints stay exact; watch time for huge ranges.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
is_armstrongstr allowedString(n).lengthd^k like the live previewQuick Takeaway: sum each digit raised to the digit-count power; if that equals n, the number is Armstrong.
| Program | Time | Extra space |
|---|---|---|
| Single check | O(log n) | O(1) |
| String-style check | O(log n) | O(log n) for the string |
| Range 1…U | about O(U log U) | O(1) |
Armstrong numbers are a clean digit-power exercise: find k, sum each digitk, and compare with n. Master the arithmetic loop first, then the string variant when you want shorter code.
Practice the three examples above, then continue to automorphic numbers for another classic digit-pattern check.
Never hard-code cubes for all cases, never overwrite n while peeling digits, and always verify 153.
intPow powersn <= 0 guardCheck digit powers the interview-friendly way.
Digit powers sum to n
Definitionk = digit count
Math% 10 and // 10
Code1³+5³+3³
ExampleO(log n) time
AnalysisFor 3-digit numbers, the Armstrong values are 153, 370, 371, and 407.
Learn how to check whether a number’s square ends with the number itself.
9 people found this page helpful