Answer to Question #200368 in C++ for Ahmed Ali

Question #200368

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:10:31-0400
#include <iostream>
#include <string>


using namespace std;


class math{
private:
	int x,y,z;
public:
	math(int x,int y,int z){
		this->x=x;
		this->y=y;
		this->z=z;
	}
	//One member function ‘max’ to find and return the maximum of three numbers
	int max(){
		int largest=x;
		int middle=y;
		int smallest=z;
		//Find the largest and the smallest number
		if(x < y) {
			largest = y;
			smallest = x;
		}else {
			largest = x;
			smallest = y;
		}
		if(largest < z) {
			largest = z;
		}    
		if(smallest > z) {
			smallest = z;
		}
		//Find the middle number
		middle=x + y + z - smallest - largest;
		return largest;
	}
	// One member function 'sum' to compute and return the sum of three numbers.
	int sum(){
		return x + y + z;
	}
	//One member function 'factorial' to find the minimum number of 
	//three numbers and computes its factorial and return to calling function.  
	int factorial(){
		int largest=x;
		int middle=y;
		int smallest=z;
		//Find the largest and the smallest number
		if(x < y) {
			largest = y;
			smallest = x;
		}else {
			largest = x;
			smallest = y;
		}
		if(largest < z) {
			largest = z;
		}    
		if(smallest > z) {
			smallest = z;
		}
		int factorial=1;
		if (smallest < 0)
			return 0;
		else {
			for(int i = 1; i <= smallest; ++i) {
				factorial *= i;
			}
		}
		return factorial;
	}
};


int main()
{


	int number1;
	int number2;
	int number3;


	cout<<"Enter number 1: ";
	cin>>number1;
	cout<<"Enter number 2: ";
	cin>>number2;
	cout<<"Enter number 3: ";
	cin>>number3;
	math _math(number1,number2,number3);


	cout << "\nThe maximum of three numbers: " << _math.max()<<"\n";    
	cout << "The sum of three numbers: " << _math.sum()<<"\n";    
	cout << "Factorial of the smallest number: " << _math.factorial()<<"\n\n";    
	system("pause");
	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