Exercise #3: Simple Calculator Write a C++ program that prompts the user to ente
ID: 3731080 • Letter: E
Question
Exercise #3: Simple Calculator Write a C++ program that prompts the user to enter two integers and a symbol for one of the four arithmetical operators (+,-, *, /, %). The program then uses the switch statement to print the result of the arithmetical operation, otherwise the program displays the message "Invalid symbol" Sample input / output: Enter two integers:59 Enter the symbol of an arithmetical operator (+, -, *, /, %): + 5+9=14 Enter two integers: 59 Enter the symbol of an arithmetical operator (+, -, *, 1, %): ^ SA9 -Invalid symbol!!!Explanation / Answer
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
char op;
int num1, num2; //variable declartion
cout << "Enter two integers: "; //enter user input
cin >> num1 >> num2;
cout << "Enter the symbol of an arithmetical operator(+,-,*,/,%):"; //enter user choice
cin >> op;
switch(op)
{
case '+':
cout <<num1<<"+"<<num2<<"="<< num1+num2;
break;
case '-':
cout <<num1<<"-"<<num2<<"="<< num1-num2;
break;
case '*':
cout<<num1<<"*"<<num2<<"=" << num1*num2;
break;
case '/':
cout <<num1<<"/"<<num2<<"="<< num1/num2;
break;
case '%':
cout<<num1<<"%"<<num2<<"="<<num1%num2;
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Invalid Symbol!!!";
break;
}
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.