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

Question #178396

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-05T07:23:38-0400
#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;
}

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