Answer to Question #286573 in C++ for Yacci

Question #286573

create a c++ program that will perform the four basic mathematical operation. design the program to choose which operation to use, then ask the user to enter two values which will be use in computation

1
Expert's answer
2022-01-11T06:05:04-0500
#include <iostream>

using namespace std;

int main()
{
   cout << "Enter a number to choose an operation:\n";
   cout << "1) +\n";
   cout << "2) -\n";
   cout << "3) *\n";
   cout << "4) /\n";
   int op;
   cin >> op;
   if (op < 1 || op > 4) {
       cout << "Incorrect operation";
       return 0;
   }

   cout << "Enter two values to compute:\n";
   int a, b;
   cout << "Value1 = ";
   cin >> a;
   cout << "Value2 = ";
   cin >> b;
   if (op == 4 && b == 0) {
       cout << "Division by zero";
       return 0;
   }
   int result;
   if (op == 1) result = a + b;
   if (op == 2) result = a - b;
   if (op == 3) result = a * b;
   if (op == 4) result = a / b;
   cout << "Result = " << result;
}

Enter only int values, without brackets


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