Armstrong Number Program in C++ | With Full Explanation & Dry Run
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 = 13+53+33 = (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 = 14+64+34+44 = (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.
using namespace std;
cout<<"Enter A Number: ";
cout<<"Not Armstrong Number.";
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<span style="color: #ff0000;">=</span>tempNum<span style="color: #ff0000;">%</span><span style="color: #993366;">10</span><span style="color: #ff0000;">;</span>
This statement extracts one digit from tempNum and stores it in digit.
result<span style="color: #ff0000;">=</span>result<span style="color: #ff0000;">+</span>pow<span style="color: #ff0000;">(</span>digit<span style="color: #ff0000;">,</span>power<span style="color: #ff0000;">);</span>
This statement raise the digit to the power of total no. of digits and stores thier sum in result.
tempNum<span style="color: #ff0000;">=</span>tempNum<span style="color: #ff0000;">/</span><span style="color: #993366;">10</span><span style="color: #ff0000;">;</span>
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.
Next, we entered a 4-digit but it is not an Armstrong number.
Let’s see how this code is executed by the computer. Suppose number entered by user is 153.
This was all about finding an Armstrong number in C++ using while loop.
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.