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.
#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;
}
Comments
Leave a comment