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. (Implement the concept of class and objects)
#include <iostream>
using namespace std;
class Armstrong{
int startnumber;
int endnumber;
public:
//Constructor
Armstrong(int startnumber , int endnumber){
this->startnumber = startnumber;
this->endnumber = endnumber;
}
// function findArmstrongNumbers() to find all the Armstrong numbers between startnumber and endnumber.
void findArmstrongNumbers(){
for(int n = this->startnumber; n <= this->endnumber; n++){
int sum = 0;
int tempNumber = n;
while(tempNumber > 0) {
int digit = tempNumber%10;
sum += pow(digit,3.0);
tempNumber /= 10;
}
//If sum of cubes of each digit of the number is equal
//to the number itself, then the number is called an Armstrong numbe
if(n == sum) {
cout<<n<<"\n";
}
}
}
};
int main(){
int startnumber;
int endnumber;
cout<<"Enter the start number: ";
cin>>startnumber;
cout<<"Enter the end number: ";
cin>>endnumber;
Armstrong armstrong(startnumber, endnumber);
armstrong.findArmstrongNumbers();
system("pause");
return 0;
}
Comments
Leave a comment