Answer to Question #321017 in C++ for sj bukhari

Question #321017

write a program in c++ to get two numbers and the operators from user:

1 if the operator is + then display sum the sum of two numbers,


1
Expert's answer
2022-03-30T14:10:03-0400
#include <iostream>
using namespace std;

int main()
{
	double num1;
	double num2;
	char operation;
	double result;

	cout << "Enter the first number: ";
	cin >> num1;

	cout << "\nEnter the second number: ";
	cin >> num2;

	while (true)
	{
		cout << "\nChoose the operation (+ - * /): ";
		cin >> operation;

		if (operation == '+' || operation == '-' || operation == '*' || operation == '/')
		{
			break;
		}

		else
		{
			cout << "\nIncorrect operation, try again" << endl;
		}
	}

	switch (operation)
	{
		case '+':
			result = num1 + num2;
			break;

		case '-':
			result = num1 - num2;
			break;

		case '*':
			result = num1 * num2;
			break;

		case '/':
			result = num1 / num2;
			break;
	}

	cout << "\nResult: " << result << 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