An Armstrong number is a number that is equal to the sum of its digits, each raised to the power of the total number of digits. In this article, we will learn how to find and print all Armstrong numbers between 1 and 1000 using C++.
- Single-digit numbers from 1 to 9 are also considered Armstrong numbers.
- The Armstrong numbers between 1 and 1000 are 1 to 9, 153, 370, 371, and 407.
Examples
Input: 1 to 1000
Output: 1 2 3 4 5 6 7 8 9 153 370 371 407

Algorithm
- Traverse all numbers from 1 to 1000.
- For each number, count the number of digits.
- Extract each digit using the modulo operator %.
- Calculate the power of each digit using the total number of digits.
- Add the calculated values.
- If the sum is equal to the original number, print the number.
- Continue until all numbers in the range have been checked.
1. Using Brute-force Approach
The idea is to first count the number of digits (or find order). Let the number of digits be order_n. For every digit curr in input number num, compute currorder_n. If the sum of all such values is equal to num, then return true, else false.
Program to find Armstrong numbers between 1 to 1000 using a brute force approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the order of
// a number.
int order(int num)
{
int count = 0;
while (num > 0)
{
num /= 10;
count++;
}
return count;
}
// Function to check whether the
// given number is Armstrong number
// or not
bool isArmstrong(int num)
{
int order_n = order(num);
int num_temp = num, sum = 0;
while (num_temp > 0)
{
int curr = num_temp % 10;
sum += pow(curr, order_n);
num_temp /= 10;
}
if (sum == num)
{
return true;
}
else
{
return false;
}
}
// Driver code
int main()
{
cout << "Armstrong numbers between 1 to 1000 : ";
// Loop which will run from 1 to 1000
for (int num = 1; num <= 1000; ++num)
{
if (isArmstrong(num))
{
cout << num << " ";
}
}
return 0;
}
Output
Armstrong numbers between 1 to 1000 : 1 2 3 4 5 6 7 8 9 153 370 371 407
Time Complexity: O(n*d), where d is the order of a number and n is the range(1, 1000).
Auxiliary Space: O(1).
2. Using Optimized Solution
The idea here is to directly print the numbers less than or equal to 9 since they are already Armstrong numbers and then use an optimized approach to check the rest numbers if they are Armstrong numbers then print them else return false.
Program to find Armstrong numbers between 1 to 1000 using an optimized solution:
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
int ord1, ord2, ord3, total_sum;
cout << "All the Armstrong numbers between 1 to 1000 : ";
// Loop which will run from 1 to 1000
for (int num = 1; num <= 1000; ++num)
{
// All the single-digit numbers are
// armstrong number.
if (num <= 9)
{
cout << num << " ";
}
else
{
ord1 = num % 10;
ord2 = (num % 100 - ord1) / 10;
ord3 = (num % 1000 - ord2) / 100;
total_sum = ((ord1 * ord1 * ord1) +
(ord2 * ord2 * ord2) +
(ord3 * ord3 * ord3));
if (total_sum == num)
{
cout << num << " ";
}
}
}
return 0;
}
All the Armstrong numbers between 1 to 1000 : 1 2 3 4 5 6 7 8 9 153 370 371 407