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 C 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 a loop dividing by 10 to count digits — that count k is the exponent for every digit.
% 10 / 10
Extract digits with modulo, add ipow(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 ipow (integer loops) — avoid math.h 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 | int | Positive integer to test (this tutorial returns false for n <= 0). |
| Return / print | int (0/1) / text | 1 / message when the digit-power sum equals n. |
function isArmstrong(n):
if n <= 0:
return 0
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 ipow(digit, k) | Interview classic; O(1) extra space |
| Power table | Precompute 0^k..9^k then look up each digit | Very readable; same O(log n) time |
| Goal | Pattern |
|---|---|
| Digit count k | count digits into k |
| Next digit | digit = temp % 10 |
| Drop last digit | temp /= 10 |
| Add powered digit | total += ipow(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; integer ipow
pows[d]Faster when you reuse the same digit count k
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 C programs — check one number, print a range, and a digit-power table variant. Click View Output to reveal sample console results.
Arithmetic digit extraction — the interview default.
Count digits, sum ipow(d, k) for each digit, then compare with the original value.
#include <stdio.h>
static int ipow(int base, int exp) {
int r = 1;
int e = exp;
while (e-- > 0) {
r *= base;
}
return r;
}
/* Returns 1 if n is Armstrong (base 10), 0 otherwise; n > 0 only */
int isArmstrong(int number) {
int k = 0;
int t = number;
if (number <= 0) {
return 0;
}
while (t > 0) {
k++;
t /= 10;
}
t = number;
int sum = 0;
while (t > 0) {
int d = t % 10;
sum += ipow(d, k);
t /= 10;
}
return sum == number;
}
int main(void) {
int number = 153;
if (isArmstrong(number)) {
printf("%d is an Armstrong number.\n", number);
} else {
printf("%d is not an Armstrong number.\n", number);
}
return 0;
} Guard non-positive inputs, count digits into k, then walk digits via a working copy so the original value stays intact for the final comparison. ipow keeps every power in integer arithmetic — no math.h pow.
Reuse the helper across a closed range.
Loop from start to end and print every value that passes the check.
#include <stdio.h>
static int ipow(int base, int exp) {
int r = 1;
int e = exp;
while (e-- > 0) {
r *= base;
}
return r;
}
int isArmstrong(int num) {
int k = 0;
int t = num;
if (num <= 0) {
return 0;
}
while (t > 0) {
k++;
t /= 10;
}
t = num;
int sum = 0;
while (t > 0) {
int d = t % 10;
sum += ipow(d, k);
t /= 10;
}
return sum == num;
}
int main(void) {
int start = 1;
int end = 200;
printf("Armstrong numbers in the range %d to %d:\n", start, end);
for (int i = start; i <= end; ++i) {
if (isArmstrong(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
} 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.
Precompute digit powers once per digit count k.
Build 0^k … 9^k once, then sum with array lookups instead of calling ipow per digit.
#include <stdio.h>
static int ipow(int base, int exp) {
int r = 1;
while (exp-- > 0) {
r *= base;
}
return r;
}
/* Precompute 0^k .. 9^k, then sum with lookups */
int isArmstrong(int number) {
int k = 0;
int t = number;
int pows[10];
int sum = 0;
int i;
if (number <= 0) {
return 0;
}
while (t > 0) {
k++;
t /= 10;
}
for (i = 0; i < 10; ++i) {
pows[i] = ipow(i, k);
}
t = number;
while (t > 0) {
sum += pows[t % 10];
t /= 10;
}
return sum == number;
}
int main(void) {
printf("%d\n", isArmstrong(153));
printf("%d\n", isArmstrong(123));
return 0;
} After counting k, fill pows[0..9] with i^k. Each digit then costs an array lookup. 153 prints 1; 123 prints 0.
If n <= 0, return 0 for this tutorial’s positive-integer definition.
Set k (power) from the number of digits in n.
Extract each digit and add ipow(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 | ipow(3, 3) = 27 | 27 |
15 | 5 | ipow(5, 3) = 125 | 152 |
1 | 1 | ipow(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 isArmstrong(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 isArmstrong 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.
ipowStay exact; floating powers can spoil equality on larger inputs.
Assert 153/370 return 1 and 123 returns 0 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 ipow.
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 0.
Extract digits from temp, compare against n.
Use long long for large power sums; watch time for huge ranges.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
isArmstrongstr allowedd^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) |
| Power-table check | O(log n) | O(log n) digit work + O(1) lookups |
| 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 power-table variant when scanning many values.
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.
ipow 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
AnalysisBesides the trivial one-digit cases 1–9, the only three-digit Armstrong numbers are 153, 370, 371, and 407. For example, 1³ + 5³ + 3³ = 153.
Learn how to check whether a number’s square ends with the number itself.
9 people found this page helpful