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 <string>
using namespace std;
class Armstrong{
int startnumber, endnumber;
public:
Armstrong(int a, int b){
startnumber = a;
endnumber = b;
}
void findArmstrongNumbers(){
int sum, a;
string temp;
for(startnumber; startnumber <= endnumber; startnumber++){
sum = 0;
temp = to_string(startnumber);
for(int i = 0; i < temp.length(); i++){
a = temp[i] - '0';
sum += a * a * a;
}
if(sum == startnumber){
cout<<startnumber<<" ";
}
}
}
};
int main(){
int a, b;
cout<<"Input startnumber: ";
cin>>a;
cout<<"Input endnumber: ";
cin>>b;
Armstrong armstrong(a, b);
armstrong.findArmstrongNumbers();
return 0;
}
Comments
Leave a comment