Answer to Question #178414 in C++ for Hari bashkar

Question #178414

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.


1
Expert's answer
2021-04-07T06:36:13-0400
#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;
}

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS