Answer to Question #193095 in C++ for Sarivinkumar M

Question #193095

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-05-13T13:52:52-0400
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

class Armstrong {
private:
    int startnumber;
    int endnumber;
public:
    Armstrong(int start, int end);
    ~Armstrong();
    void findArmstrongNumbers();
};

Armstrong::Armstrong(int start, int end) {
    this->startnumber = start;
    this->endnumber = end;
}

Armstrong::~Armstrong() {}

void Armstrong::findArmstrongNumbers() {
    cout << "\nAll the Armstrong numbers between your integers are: \n";
    
    for (int i = startnumber; i <= endnumber; ++i) {
        int x = i;
        int n = 0;

        while (x != 0) {
            x /= 10;
            ++n;
        }
 
        int powSum = 0;
        x = i;

        while (x != 0) {
            int digit = x % 10;
            powSum += pow(digit, n);
            x /= 10;
        }
 
        if (powSum == i) {
            cout << i << "\n";    
        }
    }
}

int main(){
    int start;
    int end;

    cout << "Hello! This program allows you to find all the Armstrong numbers between two integers.\n\n";

    cout << "Please enter the start number: ";
    cin >> start;
    
    cout << "Please enter the end number: ";
    cin >> end;

    Armstrong armstrong(start, end);
    
    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

Assignment Expert
14.05.21, 07:37

Dear Sarivinkumar M please post a new task

Sarivinkumar M
14.05.21, 05:36

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 Run time input 1and 500

Leave a comment

LATEST TUTORIALS
New on Blog