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 Python 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 len(str(n)) or a loop dividing by 10 — k is the exponent for every digit.
% 10 // 10
Extract digits with modulo, add 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 ** (or integer loops) — avoid float 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 | int | 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 / // 10 with digit ** k | Interview classic; O(1) extra space |
| String digits | Iterate characters of str(n) | Very readable; same O(log n) time |
| Goal | Pattern |
|---|---|
| Digit count k | power = len(str(n)) |
| Next digit | digit = temp % 10 |
| Drop last digit | temp //= 10 |
| Add powered digit | total += 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 str(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 Python programs — 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 digit ** power, then compare with the original value.
def is_armstrong(n: int) -> bool:
if n <= 0:
return False
power = len(str(n))
total = 0
temp = n
while temp > 0:
digit = temp % 10
total += digit ** power
temp //= 10
return total == n
number = 153
if is_armstrong(number):
print(f"{number} is an Armstrong number.")
else:
print(f"{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.
def is_armstrong(n: int) -> bool:
if n <= 0:
return False
power = len(str(n))
total = 0
temp = n
while temp > 0:
digit = temp % 10
total += digit ** power
temp //= 10
return total == n
start, end = 1, 200
print(f"Armstrong numbers in the range {start} to {end}:")
for value in range(start, end + 1):
if is_armstrong(value):
print(value, end=" ") 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 print.
Same math with string iteration.
Convert to a string, raise each character digit to power len(s), and compare.
def is_armstrong(n: int) -> bool:
if n <= 0:
return False
s = str(n)
power = len(s)
total = sum(int(ch) ** power for ch in s)
return total == n
print(is_armstrong(153))
print(is_armstrong(123)) len(s) is k; each character becomes an int and is raised to that power. 153 passes; 123 sums to 36 and fails.
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 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.
**Stay 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 **.
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.
Python ints stay exact; watch time for huge ranges.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
is_armstrongstr allowedlen(str(n))d^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.
** 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