Create a class Armstrong and include startnumber and endnumber as data member and aslo include member function findArmstrongNumbers() to find all the Armstrong numbers between startnumber and endnumber. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number.
#include <iostream>
#include <vector>
class Armstrong {
public:
Armstrong(int s, int e)
: start { s }
, end { e }
{}
std::vector<int> findArmstrongNumbers() const {
std::vector<int> result;
for(int i = start; i <= end; i++) {
if(isArmstrong(i)) {
result.push_back(i);
}
}
return result;
}
private:
bool isArmstrong(int x) const {
int cp { x };
int sum { 0 };
while(cp) {
int remainder = (cp % 10);
sum += remainder * remainder * remainder;
cp /= 10;
}
return sum == x;
}
int start, end;
};
int main() {
Armstrong inst { 1, 2100 };
for(auto&& x: inst.findArmstrongNumbers()) {
std::cout << x << " ";
}
return 0;
}
Comments
Leave a comment