Armstrong Number: flowchart, algorithm and program
This program reads an integer n and checks whether it equals the sum of its digits, each raised to the power of the number of digits. For example, 153 has 3 digits and 1³ + 5³ + 3³ = 153, so it is an Armstrong number.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read n.
- Step 3: Set sum = 0, count = 0, temp = n.
- Step 4: If temp > 0, go to step 5; otherwise go to step 6.
- Step 5: Set count++; temp = temp / 10. Go to step 4.
- Step 6: Set temp = n.
- Step 7: If temp > 0, go to step 8; otherwise go to step 13.
- Step 8: Set digit = temp % 10; power = 1; i = 0.
- Step 9: If i < count, go to step 10; otherwise go to step 12.
- Step 10: Set power = power * digit.
- Step 11: Increase i by 1. Go to step 9.
- Step 12: Set sum = sum + power; temp = temp / 10. Go to step 7.
- Step 13: If sum == n, go to step 14; otherwise go to step 15.
- Step 14: Print n is an Armstrong number. Go to step 16.
- Step 15: Print n is not an Armstrong number.
- Step 16: Stop.
Pseudocode
BEGIN
READ n
sum ← 0, count ← 0, temp ← n
WHILE temp > 0 DO
count++; temp ← temp / 10
END WHILE
temp ← n
WHILE temp > 0 DO
digit ← temp % 10; power ← 1; i ← 0
WHILE i < count DO
power ← power * digit
i++
END WHILE
sum ← sum + power; temp ← temp / 10
END WHILE
IF sum == n THEN
PRINT n is an Armstrong number
ELSE
PRINT n is not an Armstrong number
END IF
END
Program in C, Python and Java
Armstrong Number program in C
#include <stdio.h>
int main() {
int n, temp, digit, count = 0, power, i;
int sum = 0;
printf("Enter n: ");
scanf("%d", &n);
temp = n;
while (temp > 0) {
count++;
temp = temp / 10;
}
temp = n;
while (temp > 0) {
digit = temp % 10;
power = 1;
for (i = 0; i < count; i++) {
power = power * digit;
}
sum = sum + power;
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;
}Armstrong Number program in Python
n = int(input("Enter n: "))
temp = n
count = 0
while temp > 0:
count += 1
temp = temp // 10
temp = n
total = 0
while temp > 0:
digit = temp % 10
power = 1
for i in range(count):
power = power * digit
total = total + power
temp = temp // 10
if total == n:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")Armstrong Number program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, temp, digit, count = 0, power, i;
int sum = 0;
System.out.print("Enter n: ");
n = sc.nextInt();
temp = n;
while (temp > 0) {
count++;
temp = temp / 10;
}
temp = n;
while (temp > 0) {
digit = temp % 10;
power = 1;
for (i = 0; i < count; i++) {
power = power * digit;
}
sum = sum + power;
temp = temp / 10;
}
if (sum == n)
System.out.println(n + " is an Armstrong number");
else
System.out.println(n + " is not an Armstrong number");
}
}
Sample input: n = 153
Output: 153 is an Armstrong number
How it works
- First loop: copy n into temp and keep dividing by 10 (temp = temp / 10), adding 1 to count each time. When temp reaches 0, count holds the number of digits (3 for 153).
- Reset temp = n, then pull out the last digit with digit = temp % 10 and drop it with temp = temp / 10.
- The inner for loop multiplies power by digit exactly count times, so power = digit^count without needing pow() or math.h.
- Each power is added to sum. After all digits are used, sum holds the Armstrong total (1 + 125 + 27 = 153 for n = 153).
- Finally if (sum == n) the number is an Armstrong number, otherwise it is not. The original n is kept untouched because we only modify temp.
Dry run (trace table)
| Step | Line | Box | n | sum | count | temp | digit | power | i | Output |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||||||
| 2 | 8 | Read n | 153 | Enter n: | ||||||
| 3 | 10 | sum = 0, count = 0, temp = n | 153 | 0 | 0 | 153 | ||||
| 4 | 11 | temp > 0? | 153 | 0 | 0 | 153 | ||||
| 5 | 12 | count++; temp = temp / 10 | 153 | 0 | 1 | 15 | ||||
| 6 | 11 | temp > 0? | 153 | 0 | 1 | 15 | ||||
| 7 | 12 | count++; temp = temp / 10 | 153 | 0 | 2 | 1 | ||||
| 8 | 11 | temp > 0? | 153 | 0 | 2 | 1 | ||||
| 9 | 12 | count++; temp = temp / 10 | 153 | 0 | 3 | 0 | ||||
| 10 | 11 | temp > 0? | 153 | 0 | 3 | 0 | ||||
| 11 | 16 | temp = n | 153 | 0 | 3 | 153 | ||||
| 12 | 17 | temp > 0? | 153 | 0 | 3 | 153 | ||||
| 13 | 18 | digit = temp % 10; power = 1; i = 0 | 153 | 0 | 3 | 153 | 3 | 1 | 0 | |
| 14 | 20 | i < count? | 153 | 0 | 3 | 153 | 3 | 1 | 0 | |
| 15 | 21 | power = power * digit | 153 | 0 | 3 | 153 | 3 | 3 | 0 | |
| 16 | 20 | i++ | 153 | 0 | 3 | 153 | 3 | 3 | 1 | |
| 17 | 20 | i < count? | 153 | 0 | 3 | 153 | 3 | 3 | 1 | |
| 18 | 21 | power = power * digit | 153 | 0 | 3 | 153 | 3 | 9 | 1 | |
| 19 | 20 | i++ | 153 | 0 | 3 | 153 | 3 | 9 | 2 | |
| 20 | 20 | i < count? | 153 | 0 | 3 | 153 | 3 | 9 | 2 |
Time and space complexity
Time: O(d²) · Space: O(1). For each of the d digits of n, the inner loop runs d times; only a few integer variables are used.
Common mistakes
- Forgetting to reset temp = n before the second loop. After the first loop temp is 0, so the second loop never runs and sum stays 0.
- Using a fixed power of 3 for every number. The power must be the digit count, which is why 9474 uses 4 and not 3.
- Forgetting to set power = 1 inside the outer loop for each digit, so the powers keep multiplying across digits and give a wrong sum.
- Modifying n directly instead of using temp, then comparing sum with a changed n at the end.
Viva questions
Q1. What is an Armstrong number? Give an example that this program would accept.
It is a number equal to the sum of its digits, each raised to the power of the number of digits. For example, 153 = 1^3 + 5^3 + 3^3 = 153, and 9474 also works with power 4.
Q2. Why do you copy n into temp instead of using n directly in the loops?
The loops keep dividing by 10 until the value reaches 0, which destroys the number. I need the original n for the final comparison sum == n, so I work on the copy temp.
Q3. What does the first while loop do, and why do we need count?
It counts the digits by dividing temp by 10 repeatedly until it becomes 0, adding 1 each time. The count is the power each digit must be raised to.
Q4. Why are sum and count initialised to 0, and power reset to 1 on line 19?
sum and count are accumulators, so they start at 0 to avoid changing the total. power is a running product, so it starts at 1, because starting at 0 would make every product 0. It is reset inside the loop so each digit starts a fresh product.
Q5. What do temp % 10 and temp / 10 do here?
temp % 10 gives the last digit. temp / 10 is integer division and drops the last digit, so the number shrinks by one digit per iteration.
Q6. What does the program print for n = 0, and for a negative n such as -153?
For 0 the loops never run because the condition temp > 0 is false, so count and sum stay 0. It prints '0 is an Armstrong number', which happens to be correct by accident. For a negative n the loops are skipped, sum is 0 and it is not equal to n, so it prints 'not an Armstrong number'.
Q7. What is the time complexity of this program?
With d digits, the first loop is O(d) and the second runs d times with an inner loop of d steps, so the total is O(d^2). Since d = log10(n), this is O((log n)^2). Space is O(1).
Q8. What would change if you wanted to find all Armstrong numbers in a range, or avoid the inner power loop?
For a range, I would wrap the whole logic in an outer loop over the numbers, resetting sum and count for each one. To avoid the inner loop I could call pow() from math.h, though it returns a double and may need rounding, or I could precompute the powers of 0 to 9 once per count.
Frequently asked questions
What is an Armstrong number?
It is a number that equals the sum of its digits, each raised to the power of the number of digits. For example, 153 = 1³ + 5³ + 3³ and 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴.
Why do we count the digits first?
The power depends on how many digits the number has. A 3-digit number uses cubes, while a 4-digit number uses fourth powers, so count must be known before summing.
Does this program work for 0 and negative numbers?
No. The loops run only while temp > 0, so for 0 or negative input count and sum stay 0. Negative numbers are not treated as Armstrong numbers, and 0 would need a special case.