An Armstrong Number is the sum of its own digits each raised to the power of total number of digits.

For example, 153 is a 3-digit Armstrong Number because:

**153 = 1 ^{3}+5^{3}+3^{3} = (1*1*1) + (5*5*5) + (3*3*3) = 1+125+27 = 153**

There exists just four 3-digits Armstrong numbers which are 153, 370, 371 and 407.

**1634 **is a perfect example of 4-digit Armstrong number.

**1634 = 1 ^{4}+6^{4}+3^{4}+4^{4} = (1*1*1*1) + (6*6*6*6) + (3*3*3*3) + (4*4*4*4) = 1+1296+81+256 = 1634**

In above example, we raised the digits to the power of 4 because total no. of digits in 1634 is 4.

We should also keep in mind that all single-digit numbers are Armstrong numbers.

Recommended reading: Student Management System Project in C++ Using Linked List

## Program Logic & Structure:

In this section we talk about the logic behind the code and how we construct the program. Let’s break the logic of finding whether a number is Armstrong or not into steps:

- Find the total no. of digits in the number.
- Extract every digit from number.
- Raise each digit to the power of total no. of digits.
- Add the results obtained by raising power.

Now, let’s think how we can convert this logic into a C++ code. In my opinion, this logic can be converted into code like this:

- We can use a loop & counter variable to count the number of digits.
- We can divide the number by 10 to extract digits from integer.
- We need a
**power**function which raises the power by total. No of digits. - We can use a
**sum**variable which adds the result after every multiplication.

## C++ Source Code To Check Armstrong Number:

`#include`
#include
using namespace std;
int main()
{
int originalnum,digit,tempNum;
int result=0,power=0;
cout"Enter A Number: ";
cin>>originalnum;
tempNum=originalnum;
while(tempNum!=0)
{
tempNum=tempNum/10;
power++;
}
tempNum=originalnum;
while(tempNum!=0)
{
digit=tempNum%10;
result=result+pow(digit,power);
tempNum=tempNum/10;
}
if(result==originalnum)
{
cout"Armstrong Number.";
}
else
{
cout"Not Armstrong Number.";
}
return 0;
}

## Code Explanation:

The most important thing about this code is that it contains **math.h** file. It is a header file which contains the functions required to apply mathematical operations. In this program, **pow()** function of this file is used which calculates the power of a number.

First of all, we declared 5 integer type variables to store data. The main purpose of declaring each of them is:

**originalnum**is an integer variable used to save the original number entered by user. We will compare our resultant number with this original number to check whether it is an Armstrong number or not.**tempNum**is a variable used to manipulate the value of original number in whole program. We don’t want to change the value of**originalnum**in whole program, so we used this variable to store the value of original number where we can change it.- We will extract each digit of our original number and store it in
**digit**. **power**is a variable which saves the number by which power of each digit would be raised. Remember that it is the total no. of digits.- We need a temporary storage location to store result of our calculations, so we declared a variable named

After that, we prompted the user to enter the number and store it in **originalnum **variable. We also copied this value in **tempNum **variable.

Recommended reading: C++ Program to Find Size of Any Data Type

Next, we used a **while** loop to count the number of digits present in the entered number. Basically it divides the number by 10 and increments the **power **variable. Dividing by **10** causes decrease of one digit in original number.

Next, we again used **while** loop to find Armstrong number. There are just three statements inside the body of this loop. Let’s see them one by one:

```
digit=tempNum%10;
```

This statement extracts one digit from **tempNum **and stores it in **digit**.

```
result=result+pow(digit,power);
```

This statement raise the digit to the power of total no. of digits and stores thier sum in **result**.

```
tempNum=tempNum/10;
```

This statement divides the number by 10 so one more digit can be accessed by loop.

This loop continues until the resultant number is found.

At the end, we compare obtained number with the original number entered by user. If they become equal then it means that it is an Armstrong number. If they are not equal then it means that entered number is not an Armstrong number.

## Program Output:

Next, we entered a 4-digit but it is not an Armstrong number.

## Dry Run:

Let’s see how this code is executed by the computer. Suppose number entered by user is **153**.

**At start, we have:**

Originalnum=153

tempNum=153

result=0

power=3

**When tempNum=153**

digit=153%10=3

result=0+33=0+27=27

tempNum=153/10=15

**When tempNum=15**

digit=15%10=5

result=27+53=27+125=152

tempNum=15/10=1

**When tempNum=1**

digit=1%10=1

result=152+13=152+1=153

tempNum=1/10=0

This was all about finding an Armstrong number in C++ using** while** loop.

If you liked this article, then I would really appreciate if you follow us on __Twitter__ and __Facebook__.

If you have any question then you can ask me in comments. I would like to ans

You can also Click Here To Download Exercise Files.

The post Armstrong Number Program in C++ | With Full Explanation & Dry Run appeared first on C++ Beginner.