C++ Program to Check Armstrong Number
while loop and pow function, then compares the sum with the original number to decide if it is Armstrong.Examples
How to Think About It
Algorithm
Code
#include <iostream> #include <cmath> using namespace std; int main() { int num, originalNum, remainder, sum = 0; cout << "Enter an integer: "; cin >> num; originalNum = num; while (num != 0) { remainder = num % 10; sum += pow(remainder, 3); num /= 10; } if (sum == originalNum) cout << originalNum << " is an Armstrong number." << endl; else cout << originalNum << " is not an Armstrong number." << endl; return 0; }
Dry Run
Let's trace the input 153 through the code.
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 | Remainder | Sum | Num after division |
|---|---|---|---|
| 1 | 3 | 27 | 15 |
| 2 | 5 | 152 | 1 |
| 3 | 1 | 153 | 0 |
Why This Works
Step 1: Extract digits
Using num % 10 extracts the last digit of the number to process each digit separately.
Step 2: Calculate cube and sum
Each digit is raised to the power 3 using pow and added to sum to accumulate the total.
Step 3: Compare sum with original
If the sum of cubes equals the original number, it confirms the number is Armstrong.
Alternative Approaches
#include <iostream> #include <cmath> #include <string> using namespace std; int main() { string numStr; cout << "Enter an integer: "; cin >> numStr; int sum = 0; int originalNum = stoi(numStr); for (char c : numStr) { int digit = c - '0'; sum += pow(digit, 3); } if (sum == originalNum) cout << originalNum << " is an Armstrong number." << endl; else cout << originalNum << " is not an Armstrong number." << endl; return 0; }
#include <iostream> using namespace std; int cube(int n) { return n * n * n; } int main() { int num, originalNum, remainder, sum = 0; cout << "Enter an integer: "; cin >> num; originalNum = num; while (num != 0) { remainder = num % 10; sum += cube(remainder); num /= 10; } if (sum == originalNum) cout << originalNum << " is an Armstrong number." << endl; else cout << originalNum << " is not an Armstrong number." << endl; 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 variables are used regardless of input size, so space complexity is O(1).
Which Approach is Fastest?
Using a custom cube function is faster than pow because it avoids floating-point operations, while string conversion uses extra memory.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Using pow function | O(d) | O(1) | Simplicity and readability |
| String conversion | O(d) | O(d) | Easy digit access but uses extra memory |
| Custom cube function | O(d) | O(1) | Performance and simplicity |
pow(digit, 3) to calculate cubes easily instead of manual multiplication.