Rewrite the program written for Task-2, where instead of asking to choose between 1 through 4, user directly enters symbols like ‘+’, ‘-’, ‘*’, and ‘.’ for quitting.

Rewrite the program written for Task-2, where instead of asking to choose between 1 through 4, user directly enters symbols like ‘+’, ‘-’, ‘*’, and ‘.’ for quitting.

Solution :
include
include
using namespace std;
int main()
{
char ch;
int num1, num2, total;
//we use while loop and it closed when we press 4 "Quit"
while (true)
{
cout << "\n……………"; cout << "\n1: + " "\n2: - " "\n3: * " "\n4: . \n"; cout << "\n Please choose your option: \n"; cin >> ch;
switch (ch)
{
//this case we use for add two numbers
case '+':
cout << "Enter two numbers: \n"; cin >> num1 >> num2;
total = num1 + num2;
cout << "Sum = " << total; break; //this case we use for subtract two numbers case '-': cout << "Enter two numbers: \n"; cin >> num1 >> num2;
total = num1 - num2;
cout << "Subtraction = " << total; break; case '*': //this case we use for multiply two numbers cout << "Enter two numbers: \n"; cin >> num1 >> num2;
total = num1 * num2;
cout << "Product = " << total;
break;
case '.':
//this case we use for exit from program
cout << "Exit …" << endl;
exit(0);
default:
cout << "Wrong choice " << endl;
}
}
}

OutPut Screen

Leave a Comment

Your email address will not be published. Required fields are marked *