Answer to Question #182441 in C++ for P. Gokul

Question #182441

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-20T03:09:00-0400
#include <iostream>
#include <vector>

class Armstrong {
public:
    Armstrong(int s, int e) 
        : start { s }
        , end { e } 
    {}

    std::vector<int> findArmstrongNumbers() const {
        std::vector<int> result;
        for(int i = start; i <= end; i++) {
            if(isArmstrong(i)) {
                result.push_back(i);
            }
        }
        return result;
    }

private:
    bool isArmstrong(int x) const {
        int cp { x };
        int sum { 0 };
        while(cp) {
            int remainder = (cp % 10);
            sum += remainder * remainder * remainder;
            cp /= 10;
        }
        return sum == x;
    }

    int start, end;
};
int main() {
    Armstrong inst { 1, 2100 };
    for(auto&& x: inst.findArmstrongNumbers()) {
        std::cout << x << " ";
    }
    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