Advertisement
mdelatorre

Armstrong's numbers in C++

Sep 30th, 2020
1,379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3. int power(int x, int i) {
  4.     if (i == 0) return 1;
  5.     int xx = 1;
  6.     for (int j = 0; j < i; j++) {
  7.         xx = xx * x;
  8.     }
  9.     return xx;
  10. }
  11. int main()
  12. {
  13.     int a = 0, b = 0;
  14.     cout << "Armstrong's numbers in ";
  15.     cout << "Range: ["; cin >> a; cout << ";"; cin >> b; cout << "]" << endl;
  16.     cout << "Armstong's numbers in this range:" << endl;
  17.     for (int i = a; i <= b; i++) {
  18.         int container = 0;
  19.         int ii = i;
  20.         while (ii > 0) {
  21.             container += power(ii % 10, 3);
  22.             ii = ii / 10;
  23.         }
  24.         if (i == container) {
  25.             cout << i << endl;
  26.         }
  27.     }
  28. }  
  29.  
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement