mdelatorre icon

Armstrong's numbers in C++

mdelatorre | PRO | 10/01/20 02:48:18 AM UTC | 0 ⭐ | 1806 👁️ | Never ⏰ | []
C++ |

680 B

|

None

|

0 👍

/

0 👎

#include <iostream>
using namespace std;
int power(int x, int i) {
    if (i == 0) return 1;
    int xx = 1;
    for (int j = 0; j < i; j++) {
        xx = xx * x;
    }
    return xx;
}
int main()
{
    int a = 0, b = 0;
    cout << "Armstrong's numbers in ";
    cout << "Range: ["; cin >> a; cout << ";"; cin >> b; cout << "]" << endl;
    cout << "Armstong's numbers in this range:" << endl;
    for (int i = a; i <= b; i++) {
        int container = 0;
        int ii = i;
        while (ii > 0) {
            container += power(ii % 10, 3);
            ii = ii / 10;
        }
        if (i == container) {
            cout << i << endl;
        }
    }
}   
 
 

Comments