C Program to Check Armstrong Number with Output
while loop and % and / operators, then compares the sum with the original number to decide if it is Armstrong.Examples
How to Think About It
% 10 to get the last digit and / 10 to remove it. Cube each digit and add to a sum. If the sum equals the original number, it is Armstrong.Algorithm
Code
#include <stdio.h> int main() { int num, originalNum, remainder, sum = 0; printf("Enter an integer: "); scanf("%d", &num); originalNum = num; while (num > 0) { remainder = num % 10; sum += remainder * remainder * remainder; num /= 10; } if (sum == originalNum) printf("%d is an Armstrong number.\n", originalNum); else printf("%d is not an Armstrong number.\n", originalNum); return 0; }
Dry Run
Let's trace the number 153 through the code to check if it is Armstrong.
Input and Initialization
num = 153, originalNum = 153, sum = 0
First loop iteration
remainder = 153 % 10 = 3; sum = 0 + 3^3 = 27; num = 153 / 10 = 15
Second loop iteration
remainder = 15 % 10 = 5; sum = 27 + 5^3 = 27 + 125 = 152; num = 15 / 10 = 1
Third loop iteration
remainder = 1 % 10 = 1; sum = 152 + 1^3 = 152 + 1 = 153; num = 1 / 10 = 0
Comparison
sum = 153 equals originalNum = 153, so number is Armstrong
| Iteration | num | remainder | sum |
|---|---|---|---|
| 1 | 153 | 3 | 27 |
| 2 | 15 | 5 | 152 |
| 3 | 1 | 1 | 153 |
Why This Works
Step 1: Extract digits
Using % 10 extracts the last digit of the number to process each digit separately.
Step 2: Calculate cube and sum
Each digit is cubed and added to a running total to check the Armstrong condition.
Step 3: Compare sums
If the sum of cubes equals the original number, it confirms the number is Armstrong.
Alternative Approaches
#include <stdio.h> int isArmstrong(int num) { int originalNum = num, sum = 0, remainder; while (num > 0) { remainder = num % 10; sum += remainder * remainder * remainder; num /= 10; } return sum == originalNum; } int main() { int num; printf("Enter an integer: "); scanf("%d", &num); if (isArmstrong(num)) printf("%d is an Armstrong number.\n", num); else printf("%d is not an Armstrong number.\n", num); return 0; }
#include <stdio.h> #include <math.h> int main() { int num, originalNum, remainder, sum = 0; printf("Enter an integer: "); scanf("%d", &num); originalNum = num; while (num > 0) { remainder = num % 10; sum += (int)pow(remainder, 3); num /= 10; } if (sum == originalNum) printf("%d is an Armstrong number.\n", originalNum); else printf("%d is not an Armstrong number.\n", originalNum); return 0; }
Complexity: O(d) time, O(1) space
Time Complexity
The program loops through each digit once, so time depends on the number of digits d, making it O(d).
Space Complexity
Only a few integer variables are used, so space complexity is constant O(1).
Which Approach is Fastest?
Direct arithmetic operations are faster than using pow() from math library due to function call overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Direct arithmetic | O(d) | O(1) | Simple and fast for small powers |
| Function with pow() | O(d) | O(1) | Flexible for different powers but slower |
| Function modular design | O(d) | O(1) | Better code organization and reuse |