Armstrong Number Program in C with Explanation
Check whether a number equals the sum of its digits each raised to the number of digits.
- Time
O(d^2) per number with the simple power loop, - Space
O(1) - Hints3
- Read3 min
An Armstrong number equals the sum of its digits, each raised to the power of the number of digits. 153 is one because 1^3 + 5^3 + 3^3 = 153. In C, count the digits, then take each digit with n % 10, add digit^k to a sum, remove it with n / 10, and compare the sum with the original number. Between 1 and 1000 the Armstrong numbers are 1 to 9, 153, 370, 371 and 407.
Problem statement
Write an Armstrong number program in C. Given a positive integer n, print
whether it is an Armstrong number.
A number with k digits is an Armstrong number if the sum of each digit raised to
the power k equals the number. Start with the 3-digit version that labs usually
ask for, then write one that works for any number of digits.
A positive integer n.
"n is an Armstrong number" or "n is not an Armstrong number".
Examples
n = 153153 is an Armstrong number1 + 125 + 27 = 153.
n = 94749474 is an Armstrong number4 digits, so use power 4: 6561 + 256 + 2401 + 256 = 9474.
n = 123123 is not an Armstrong number1 + 8 + 27 = 36, which is not 123.
Constraints
- 1 <= n <= 99999999. For 9 or more digits the sum can pass the int limit, so use long long
Think before you code
Reveal the questions to ask yourself first
- How do you take out the digits of a number one by one?
- What power should each digit be raised to?
- The loop destroys
n. What do you compare with at the end?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
Armstrong number logic
| Number | Digits k | Calculation | Sum | Armstrong? |
|---|---|---|---|---|
| 153 | 3 | 1³ + 5³ + 3³ | 153 | Yes |
| 370 | 3 | 3³ + 7³ + 0³ | 370 | Yes |
| 123 | 3 | 1³ + 2³ + 3³ | 36 | No |
| 9474 | 4 | 9⁴ + 4⁴ + 7⁴ + 4⁴ | 9474 | Yes |
Steps
- Count the digits
k. - Copy
nintotemp, setsum = 0. - While
temp > 0:d = temp % 10, adddto the powerktosum,temp = temp / 10. - If
sum == n, it is an Armstrong number.
The digit loop is the same one used for sum of digits and reverse a number.
Dry run
Walk through the example step by step
n = 153, k = 3, temp = 153, sum = 0.
temp | d = temp % 10 | d^3 | sum | temp / 10
-----+---------------+-----+-----+----------
153 | 3 | 27 | 27 | 15
15 | 5 | 125 | 152 | 1
1 | 1 | 1 | 153 | 0
sum == n, so 153 is an Armstrong number.
Solution
Reveal the full C solution
#include <stdio.h>
int countDigits(int n) {
int count = 0;
do {
count++;
n /= 10;
} while (n > 0);
return count;
}
int power(int base, int exp) {
int result = 1;
for (int i = 0; i < exp; i++) {
result *= base;
}
return result;
}
int isArmstrong(int n) {
int k = countDigits(n);
int temp = n, sum = 0;
while (temp > 0) {
sum += power(temp % 10, k);
temp /= 10;
}
return sum == n;
}
int main(void) {
int tests[] = {153, 370, 9474, 123};
for (int i = 0; i < 4; i++) {
if (isArmstrong(tests[i])) {
printf("%d is an Armstrong number\n", tests[i]);
} else {
printf("%d is not an Armstrong number\n", tests[i]);
}
}
printf("Armstrong numbers from 1 to 1000: ");
for (int n = 1; n <= 1000; n++) {
if (isArmstrong(n)) {
printf("%d ", n);
}
}
printf("\n");
return 0;
}Output:
153 is an Armstrong number
370 is an Armstrong number
9474 is an Armstrong number
123 is not an Armstrong number
Armstrong numbers from 1 to 1000: 1 2 3 4 5 6 7 8 9 153 370 371 407
countDigitsusesdo-whileso that it returns 1 forn = 0as well.powermultiplies in a loop and stays in integers.- Every single-digit number is an Armstrong number, because
dto the power 1 isd.
Armstrong number in C for 3 digits (lab version)
Many lab manuals ask only for 3-digit numbers, so the power is fixed at 3:
#include <stdio.h>
int main(void) {
int n, temp, sum = 0;
printf("Enter a 3-digit number: ");
scanf("%d", &n);
temp = n;
while (temp > 0) {
int d = temp % 10;
sum = sum + d * d * d;
temp = temp / 10;
}
if (sum == n) {
printf("%d is an Armstrong number\n", n);
} else {
printf("%d is not an Armstrong number\n", n);
}
return 0;
}
Enter a 3-digit number: 153
153 is an Armstrong number
Enter a 3-digit number: 123
123 is not an Armstrong number
This version is correct only for 3-digit input. For 1634 it would use cubes and say "not Armstrong", even though 1634 is one with power 4.
Armstrong numbers in a range
| Range | Armstrong numbers |
|---|---|
| 1 to 9 | 1, 2, 3, 4, 5, 6, 7, 8, 9 |
| 100 to 999 | 153, 370, 371, 407 |
| 1000 to 9999 | 1634, 8208, 9474 |
There are no 2-digit Armstrong numbers.
Why not use pow()?
pow() from math.h works with double. On some older compilers, a result like pow(5, 3)
can come out as 124.999..., which becomes 124 when stored in an int. A small int loop
avoids that. With gcc you also need -lm to link math.h on many systems.
The Java version is on check whether a number is an Armstrong number. For more C practice, see loop programs in C.
O(d^2) per number with the simple power loop, where d is the number of digitsO(1)Review before the interview
Common mistakes
- Comparing sum with temp after the loop. temp is 0 by then; compare with n.
- Always cubing the digits. That only works for 3-digit numbers.
- Forgetting to reset sum to 0 for each number when checking a range.
- Using pow() and storing the double in an int, which can round down.
Edge cases to test
- n = 0: the while loop does not run, sum is 0, so 0 counts as an Armstrong number. Some teachers start from 1.
- Single-digit numbers are all Armstrong numbers.
- Numbers with a 0 digit, such as 370 and 407, still work because 0 to any power is 0.
- Negative numbers are not Armstrong numbers in the usual definition.
Interview follow-ups
- Can you print all Armstrong numbers between two numbers entered by the user?
- How would you make the power calculation faster by storing powers of 0 to 9?
- What is the difference between an Armstrong number and a strong number?
Keep practising
Check Whether a Number Is an Armstrong Number
Hints · Dry run · SolutionSolve Similar problemPrint All Armstrong Numbers Between 1 and 1000
Hints · Dry run · SolutionSolve Similar problemCheck Whether a Number Is a Strong Number
Hints · Dry run · SolutionSolve Similar problemCount the Number of Digits in a Number
Hints · Dry run · SolutionSolve Related learningLearn C Programming Step By Step
TutorialLearn Related learningFunctions For Beginners
TutorialLearn Related learningC Programming Interview Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartBuilding your programming basics?
CodeBegun’s programs build problem-solving with mentor review and mock interviews, online or in Madhapur, Hyderabad.
