Armstrong Number
Program to check if a number is an Armstrong number
C++ Armstrong Number Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int num, original, remainder, result = 0, n = 0;
cout << "Enter a number: ";
cin >> num;
original = num;
// Count number of digits
int temp = num;
while (temp != 0) {
temp /= 10;
n++;
}
temp = num;
// Calculate sum of digits raised to power n
while (temp != 0) {
remainder = temp % 10;
result += pow(remainder, n);
temp /= 10;
}
if (result == original) {
cout << original << " is an Armstrong number" << endl;
} else {
cout << original << " is not an Armstrong number" << endl;
}
return 0;
}Enter a number: 153 153 is an Armstrong number
Understanding Armstrong Number
This program checks whether a number is an Armstrong number (also called Narcissistic number). An Armstrong number is a number that equals the sum of its digits, each raised to the power of the number of digits. This program demonstrates digit extraction, counting digits, exponentiation, and accumulation patterns.
---
1. What is an Armstrong Number?
An Armstrong number is a number where the sum of its digits, each raised to the power of the number of digits, equals the number itself.
Mathematical definition:
Examples:
153 (3 digits):
153
✅
371 (3 digits):
371
✅
9474 (4 digits):
9474
✅
123 (3 digits):
36
❌
Single digit numbers:
5
✅
---
2. Header Files Used
#include <iostream>
cout for output and cin for input#include <cmath>
pow() function for exponentiation---
3. Declaring Variables
int num, original, remainder, result = 0, n = 0;
Variable `num`:
Variable `original`:
Variable `remainder`:
Variable `result`:
-
Initialized to 0
Variable `n`:
-
Initialized to 0
Why initialize result and n to 0?
result needs to start at 0 for additionn needs to start at 0 for counting---
4. Taking Input From User
cin >> num;
Example:
153
num = 153---
5. Saving Original Number
original = num;
Why save original?
num will be modified (reduced to 0) during processing---
6. Counting the Number of Digits
int temp = num;
while (temp != 0)
temp /= 10;
n++;
Why count digits first?
Step-by-step (for num = 153):
Iteration 1:
temp = 153temp = 153 / 10 = 15 (integer division)n = 1Iteration 2:
temp = 15temp = 15 / 10 = 1n = 2Iteration 3:
temp = 1temp = 1 / 10 = 0n = 3Iteration 4:
temp != 0 → 0 != 0 →false
-
Loop stops
Result:
n = 3 (153 has 3 digits) ✅
---
7. Calculating Sum of Digits Raised to Power n
temp = num; (reset temp to original number)
while (temp != 0)
remainder = temp % 10;
result += pow(remainder, n);
temp /= 10;
This extracts each digit and raises it to power n, then adds to result.
Step-by-step (for num = 153, n = 3):
Iteration 1:
remainder = 153 % 10 = 3pow(3, 3) = 27result = 0 + 27 = 27temp = 153 / 10 = 15Iteration 2:
remainder = 15 % 10 = 5pow(5, 3) = 125result = 27 + 125 = 152temp = 15 / 10 = 1Iteration 3:
remainder = 1 % 10 = 1pow(1, 3) = 1result = 152 + 1 = 153temp = 1 / 10 = 0Iteration 4:
temp != 0 →false
-
Loop stops
Result:
result = 153 ✅
---
8. Checking if Number is Armstrong
if (result == original)
The key comparison:
Armstrong number
✅
Not an Armstrong number
❌
For our example:
result = 153original = 153153 == 153 →true
→
Armstrong number
✅
---
9. Complete Example Walkthrough
Input:
153
Step 1: Count digits
n = 3Step 2: Calculate sum
result = 153Step 3: Compare
result (153) == original (153) →true
Step 4: Output
-
"153 is an Armstrong number"
✅
---
10. More Examples
Example 1: 371 (Armstrong)
371
✅
Example 2: 9474 (Armstrong)
9474
✅
Example 3: 123 (Not Armstrong)
36
❌
---
11. Why Two Passes Through the Number?
First pass:
Count digits
Second pass:
Calculate sum
Why not combine?
---
12. Edge Cases
Case 1: Single digit numbers
Case 2: Number 0
Case 3: Large numbers
---
Summary
pow() function for exponentiationThis program teaches:
Understanding Armstrong numbers helps in:
Armstrong numbers are a fascinating mathematical concept, and this program demonstrates how to efficiently check for them using digit manipulation and exponentiation.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Armstrong Number
This C++ program is part of the "Loop Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.