Answer to Question #297168 in C++ for Princess faith Ner

Question #297168

Make a program that will accept an arithmetic operators (either +,-,*,/ and %) and two numbers to compute. It has to display the correct answer based on the operator you enter.




Noted: Since there are 5 arithmetic operators to choose from, there should be cases followed by the default.

1
Expert's answer
2022-02-13T09:42:36-0500
#include <iostream>




using namespace std;




int main() {
	double number1;
	double number2;
	char operation;
	cout<<"Enter the first number: ";
	cin>>number1;
	cout<<"Enter the second number: ";
	cin>>number2;
	cout<<"Enter the mathematical operation (+, -, *, /): ";
	cin>>operation;
	switch (operation) {
	case '+': {
		double result = number1 + number2;
		cout<<number1<<" + "<<number2<<" = "<<result<<"\n\n";
		break;
			  }
	case '-': {
		double result = number1 - number2;
		cout<<number1<<" - "<<number2<<" = "<<result<<"\n\n";
		break;
			  }
	case '*': {
		double result = number1 * number2;
		cout<<number1<<" * "<<number2<<" = "<<result<<"\n\n";
		break;
			  }
	case '/': {
		double result = number1 / number2;
		cout<<number1<<" / "<<number2<<" = "<<result<<"\n\n";
		break;
			  }
	case '%': {
		int result = (int)number1 % (int)number2;
		cout<<number1<<" % "<<number2<<" = "<<result<<"\n\n";
		break;
			  }
	default:
		{
			double result = number1 + number2;
			cout<<number1<<" + "<<number2<<" = "<<result<<"\n\n";
			break;
		}
		break;
	}


	cin>>number1;
	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