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
n
Digit powers
Result
153
1³+5³+3³ = 153
Armstrong
370
3³+7³+0³ = 370
Armstrong
123
1³+2³+3³ = 36
Not Armstrong
5
5¹ = 5
Armstrong
Approach
How to Check an Armstrong Number in C
Four clear steps — count digits first, then sum powers with that same exponent.
Reject n <= 0 for this page’s positive-integer rule.
Count digits into k (repeatedly divide by 10).
Peel each digit with % 10 and add digit^k.
Compare the sum with the original number.
Method
Idea
Time
Best for
Digit loop + ipow
Count k, then sum d^k
O(log n)
Interviews, default
Power table
Precompute 0^k..9^k
O(log n)
Many checks with same k
Range scan
Reuse the helper
O(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
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Test
sum(digit^k) == n, k = digits
Peel digit
d = n % 10, then n /= 10
Powers
Use integer ipow, not pow()
1 digit
1–9 are Armstrong
3 digits
153, 370, 371, 407
1..200
1–9 and 153
Try it
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.
Start with a single check using integer powers. Then list a range, and finish with a digit-power lookup table.
Example 1 — Check One Number
Count digits, sum ipow(d, k) for each digit, then compare with the original value.
C
#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;
int sum = 0;
if (number <= 0) {
return 0;
}
while (t > 0) {
k++;
t /= 10;
}
t = number;
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;
}
Output
153 is an Armstrong number.
How It Works
Return 0 for non-positive inputs.
Count digits into k with a working copy t.
Peel digits again and add ipow(d, k) each time.
Compare sum with the original number.
ipow stays in integer math — no math.hpow.
Example 2 — Armstrong Numbers from 1 to 200
Reuse the helper and print every match in a closed range.
C
#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;
int sum = 0;
if (num <= 0) {
return 0;
}
while (t > 0) {
k++;
t /= 10;
}
t = num;
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;
}
Output
Armstrong numbers in the range 1 to 200:
1 2 3 4 5 6 7 8 9 153
How It Works
Loop from start to end inclusive.
Call isArmstrong(i) for each value.
Single-digit numbers print first (each is Armstrong).
153 is the only other hit in 1…200.
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.
C
#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;
}
Output
1
0
How It Works
Count digits into k as before.
Fill pows[0..9] with i^k once.
Each digit then costs an array lookup instead of a fresh power loop.
153 prints 1; 123 prints 0.
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.
pow()
Prefer integer powers
Floating 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.
Overflow
Use a wider sum
For large n, accumulate with long long so digit powers do not wrap.
Analysis
Time and Space Complexity
Program
Time
Extra space
Single check
O(log n)
O(1)
Range of size N
O(N log N)
O(1)
Power table check
O(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.
Remember
Key Takeaways
Definition: sum of each digit raised to the digit count equals n.
Classic: 153 = 1³ + 5³ + 3³.
Powers: use integer ipow; avoid floating pow.
Test set: 153, 370, 9 yes; 123 no.
One line: raise every digit to the digit count — if the sum equals n, it is Armstrong.
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.
pow uses floating point and can round incorrectly. Integer exponentiation stays exact for typical interview sizes and needs no -lm link.
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).