In this post, we will check whether a number is Armstrong or not in C++ and will also print all Armstrong numbers from 1 to 1000.

An Armstrong number is that number in which sum of the nth power of each digit is equal to the original number, where n is the number of digits in the number.

Armstrong number in C++

To check Armstrong number in C++, we will have to implement the following steps.

  1. Input the number (num).
  2. Count the number of digits in the number (length).
  3. Sum the digits raised to the power length and store in sum.
  4. Check if sum == num.
  5. If YES then its Armstrong Number, Else not.

Let’s see the C++ source code.

output

Armstrong Number in C++ Between 1 to 1000

We now know how to check Armstrong’s number in C++.

Now Let’s use the same logic of the above program to check each number from 1 to 100 and output it if its Armstrong.

output

We have created a separate function isArmstrong() which returns true if the passed number is armstrong else it returns false.

Depending on the incoming result from ‘isArmstrong()’ method we output the number i.e if the returned value is True.

Comment below if you have not understood any part of the code or you have any suggestions.

Leave a Reply