In: Computer Science
Q-1) Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result. For division, if the denominator is zero, output an appropriate message.
Some sample outputs follow:
3 + 4 = 7
13 * 5 = 65
#include <iostream>
//Preprocessing required libraries which are pre-defined
using namespace std;
//Exicution starts with main function which is mandatory
int main()
{
//Declaring variables
int number1, number2;
char operation;
//Printing
cout << "Enter 1st Number: ";
//Taking input from the user
cin >> number1;
cout << "Enter 2nd Number: ";
cin >> number2;
cout << "Enter Operator: ";
cin>> operation ;
//Navigating based on operation provided by the
user using switch
switch (operation) {
case '+':
cout << number1 <<operation <<number2 <<" =
" << number1 + number2 <<endl;
//Breaking the switch
break;
case '-':
cout << number1 <<operation <<number2 <<" =
" << number1 - number2 << endl;
break;
case '*':
cout << number1 <<operation <<number2 <<" =
" << number1 * number2 << endl;
break;
case '/':
if(number2==0){
cout << "Cannot divide by zero" <<
endl;
break;
}
else{
cout <<
number1 <<operation <<number2 <<" = " <<
number1 / number2 << endl;
break;
}
//Printing default message
default:
cout << "Error! Please enter correct input values" <<
endl;
return 1;
break;
}
return 0;
}
Output Screenshot: