Summary: In this programming example, we’ll check whether a number is Armstrong or not in C++ and will also learn to print all Armstrong numbers in the given range.

An Armstrong number is the number that is equal to the sum of digits raise to the power total number of digits in the number.

Armstrong number in C++

To check Armstrong number in C++:

  1. Input the number (num).
  2. Count the number of digits in the number (length).
  3. Add the digits raised to the power length and store in sum.
  4. Check if sum == num. If yes, then it’s Armstrong Number, else not.

Here is the full C++ program that checks the input number for Armstrong number:

Output:

Enter a number: 370
Armstrong number


Print all the Armstrong Numbers in the Given Range

To check all Armstrong numbers in a given interval, we will write the logic used in the above program inside a different method and check by passing each number into that function.

Here is the C++ program which prints all Armstrong numbers from 1 to 1000:

Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407

In this program, we have created a separate function isArmstrong() that returns true if the passed number is an Armstrong otherwise, it returns false.

In the main method, we iterate through all the numbers in the range 1 to 1000 and pass them to the isArmstrong() method for the check.

In this tutorial, we learned to check Armstrong’s number in the C++ programming language.

Leave a Reply

four × five =