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,
#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;
}
Comments
Leave a comment