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

Expert's answer

#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


LATEST TUTORIALS
APPROVED BY CLIENTS