Armstrong Number

Program to check if a number is an Armstrong number

IntermediateTopic: Loop Programs
Back

C++ Armstrong Number Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#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;
}
Output
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:

For a number with n digits: If sum of (each digit^n) = the number, then it's an Armstrong number.

Examples:

153 (3 digits):

1³ + 5³ + 3³ = 1 + 125 + 27 =

153

153 is an Armstrong number

371 (3 digits):

3³ + 7³ + 1³ = 27 + 343 + 1 =

371

371 is an Armstrong number

9474 (4 digits):

9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 =

9474

9474 is an Armstrong number

123 (3 digits):

1³ + 2³ + 3³ = 1 + 8 + 27 =

36

36 ≠ 123, so 123 is NOT an Armstrong number

Single digit numbers:

All single digit numbers (0-9) are Armstrong numbers
Example: 5 = 5¹ =

5

---

2. Header Files Used

#include <iostream>

Provides cout for output and cin for input

#include <cmath>

Provides pow() function for exponentiation
Essential for raising digits to power n

---

3. Declaring Variables

int num, original, remainder, result = 0, n = 0;

Variable `num`:

Stores the number entered by the user
Will be modified during processing

Variable `original`:

Saves the original number
Needed for comparison and display

Variable `remainder`:

Temporarily stores each digit as we extract it

Variable `result`:

Accumulates the sum of (digit^n)

-

Initialized to 0

starting point for addition

Variable `n`:

Stores the number of digits

-

Initialized to 0

will count digits

Why initialize result and n to 0?

result needs to start at 0 for addition
n needs to start at 0 for counting

---

4. Taking Input From User

`cout << "Enter a number: ";`

cin >> num;

Prompts user to enter a number
Reads and stores it

Example:

User enters:

153

num = 153

---

5. Saving Original Number

original = num;

Why save original?

num will be modified (reduced to 0) during processing
We need original for:
1.Counting digits (first pass)
2.Extracting digits (second pass)
3.Comparison with result
4.Display in output

---

6. Counting the Number of Digits

int temp = num;

while (temp != 0)

temp /= 10;

n++;

Why count digits first?

We need to know how many digits the number has
This determines the power to which we raise each digit
Example: 153 has 3 digits, so we raise each digit to power 3

Step-by-step (for num = 153):

Iteration 1:

temp = 153
temp = 153 / 10 = 15 (integer division)
n = 1

Iteration 2:

temp = 15
temp = 15 / 10 = 1
n = 2

Iteration 3:

temp = 1
temp = 1 / 10 = 0
n = 3

Iteration 4:

Check: temp != 00 != 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:

Extract: remainder = 153 % 10 = 3
Calculate: pow(3, 3) = 27
Add: result = 0 + 27 = 27
Remove: temp = 153 / 10 = 15

Iteration 2:

Extract: remainder = 15 % 10 = 5
Calculate: pow(5, 3) = 125
Add: result = 27 + 125 = 152
Remove: temp = 15 / 10 = 1

Iteration 3:

Extract: remainder = 1 % 10 = 1
Calculate: pow(1, 3) = 1
Add: result = 152 + 1 = 153
Remove: temp = 1 / 10 = 0

Iteration 4:

Check: temp != 0

false

-

Loop stops

Result:

result = 153

---

8. Checking if Number is Armstrong

if (result == original)

The key comparison:

If sum of (digits^n) equals original number →

Armstrong number

Otherwise →

Not an Armstrong number

For our example:

result = 153
original = 153
153 == 153

true

Armstrong number

---

9. Complete Example Walkthrough

Input:

153

Step 1: Count digits

Process: 153 → 15 → 1 → 0
Count: 3 digits
n = 3

Step 2: Calculate sum

Extract digits: 3, 5, 1
Calculate: 3³ + 5³ + 1³ = 27 + 125 + 1 = 153
result = 153

Step 3: Compare

result (153) == original (153)

true

Step 4: Output

-

"153 is an Armstrong number"

---

10. More Examples

Example 1: 371 (Armstrong)

Digits: 3, 7, 1 (3 digits)
Calculation: 3³ + 7³ + 1³ = 27 + 343 + 1 =

371

Example 2: 9474 (Armstrong)

Digits: 9, 4, 7, 4 (4 digits)
Calculation: 9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 =

9474

Example 3: 123 (Not Armstrong)

Digits: 1, 2, 3 (3 digits)
Calculation: 1³ + 2³ + 3³ = 1 + 8 + 27 =

36

36 ≠ 123 → Not an Armstrong number

---

11. Why Two Passes Through the Number?

First pass:

Count digits

We need to know n (number of digits)
This determines the power

Second pass:

Calculate sum

Extract each digit
Raise to power n
Add to result

Why not combine?

We need n before we can calculate powers
Must count first, then calculate

---

12. Edge Cases

Case 1: Single digit numbers

All 0-9 are Armstrong numbers
Example: 5 = 5¹ = 5 ✅

Case 2: Number 0

0 = 0¹ = 0 ✅ (Armstrong)

Case 3: Large numbers

9474, 54748, 92727 are larger Armstrong numbers
Program handles them correctly

---

Summary

Armstrong number: sum of (each digit^n) = the number itself
Algorithm: Count digits, then calculate sum of (digit^n)
Use pow() function for exponentiation
Compare result with original number
Two passes needed: first to count, second to calculate

This program teaches:

Digit extraction and counting
Exponentiation using pow()
Accumulation pattern
Two-pass algorithm design
Number manipulation techniques

Understanding Armstrong numbers helps in:

Number theory problems
Pattern recognition
Algorithm design
Many mathematical programming challenges

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.

Table of Contents