Answer to Question #193010 in C++ for Madeshwaran

Question #193010

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)


1
Expert's answer
2021-05-15T07:31:34-0400
#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;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment