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>
using namespace std;
class Armstrong{
int startnumber, endnumber;
public:
Armstrong(int num1, int num2){
if(num1 < num2){
startnumber = num1;
endnumber = num2;
}
else{
startnumber = num2;
endnumber = num1;
}
}
void findArmstrongNumbers(){
int sum, tmp, rem, number;
number = startnumber;
for(number; number <= endnumber; number++){
sum = 0;
tmp = number;
while(tmp > 0) {
rem = tmp%10;
sum = sum+(rem*rem*rem);
tmp = tmp/10;
}
if(number == sum)
cout<<number<<endl;
}
}
};
int main(){
int n1, n2;
cout<<"Input StartNumber: ";
cin>>n1;
cout<<"Input EndNumber: ";
cin>>n2;
Armstrong armstrong(n1, n2);
armstrong.findArmstrongNumbers();
return 0;
}
Comments
Leave a comment