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.