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.

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:

``````#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int sum=0, num, digit;

cout<<"Enter a number: ";
cin >> num;

//Count the digits (i.e length)
int length = to_string(num).length();

//Copy the original number
int temp = num;

//add digits raised to the power length
while(temp != 0){
digit = temp%10;
sum += floor(pow(digit, length));
temp = temp/10;
}

if(sum == num)
cout << "Armstrong number \n";
else
cout << "Not an Armstrong number \n";

return 0;
}``````

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:

``````#include <iostream>
#include <cmath>
using namespace std;

//Function return true is 'num' is Armstrong
bool isArmstrong(int num){
//Count the digits (i.e length)
int length = to_string(num).length();

//Copy the original number
int temp = num;

//add digits raised to the power length
int digit, sum= 0;
while(temp != 0){
digit = temp%10;
sum += floor(pow(digit, length));
temp = temp/10;
}

return sum == num;
}

int main()
{
for(int i=1; i<=1000; i++){
if(isArmstrong(i))
cout << i << ", ";
}
return 0;
}``````

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.