Answer to Question #200372 in C++ for hamza

Question #200372

Write a program using class “math”. Pass three values as arguments to the constructor of the class and initialize these values to data members x&y&z of the class . the class should have the following member functions-: o One member function ‘max’ to find and return the maximum of three numbers. o One member function ‘sum’ to compute and return the sum of three numbers. o One member function ‘factorial’ to find the minimum number of three numbers and computes its factorial and return to calling function.


1
Expert's answer
2021-05-30T07:12:47-0400
#include <iostream>
using namespace std;

class math {
private:
        int x;
        int y;
        int z;
public:
        math(int x, int y, int z) {
                this->x = x;
                this->y = y;
                this->z = z;
        }
        int max() {
                if (x >= y && x >= z)
                        return x;
                if (y >= x && y >= z)
                        return y;
                return z;
        }
        int sum() {
                return x + y + z;
        }
        int factorial() {
                int min;
                int fact = 1;
                if (x <= y && x <= z)
                        min = x;
                if (y <= x && y <= z)
                        min = y;
                if (z <= x && z <= y)
                        min = z;
                if (min == 0)
                        return 1;
                for (int i = 1; i <= min; i++)
                        fact = fact * i;
                return fact;
        }
};

int main(){
    math m(5, 8, 3);
    cout<<"Max: " << m.max() << endl;
    cout<<"Sum: " << m.sum() << endl;
    cout<<"Factorial: " << m.factorial() << endl;

    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