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:

  1. Find the total no. of digits in the number.
  2. Extract every digit from number.
  3. Raise each digit to the power of total no. of digits.
  4. 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:

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

C++ Source Code To Check Armstrong Number:

using namespace std;
int main()
	int originalnum,digit,tempNum;
	int result=0,power=0;
	cout<<"Enter A Number: ";
		cout<<"Armstrong Number.";
		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:

  1. 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.
  2. 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.
  3. We will extract each digit of our original number and store it in digit.
  4. 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.
  5. 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:


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


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


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:

Armstrong Number Program in C++

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

C++ Armstrong Number Program with ouput

Dry Run:

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

At start, we have:


When tempNum=153


When tempNum=15


When tempNum=1


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.