How to use Optimized Solution In C++

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.

Below is a C++ program to find Armstrong numbers between 1 to 1000 using an optimized solution:

C++




// C++ 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 

Time Complexity: O(n).
Auxiliary Space: O(1).



C++ Program to Print Armstrong Numbers Between 1 to 1000

Here, we will see how to print Armstrong numbers between 1 to 1000 using a C++ program. 

Similar Reads

Armstrong Number

A number “N” is an Armstrong number if “N” is equal to the sum of all N’s digits raised to the power of the number of digits in N....

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....

2. Using Optimized Solution

...