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 output as follow
3 + 4 = 7
13 * 5 = 65
#include <iostream>
using namespace std;
int main()
{
int operand1, operand2;
char operator;
cout << "Enter the first operand: ";
cin >> operand1;
cout << "Enter the second operand: ";
cin >> operand2;
cout << "Enter the operator(+, -, *, /): ";
cin >> operator;
if(operator == '+')
{
cout << opreand1 << " + " << operand2 << " = " << operand1 + operator2 << endl;
}
else if(operator == '-')
{
cout << opreand1 << " - " << operand2 << " = " << operand1 - operator2 << endl;
}
else if(operator == '*')
{
cout << opreand1 << " * " << operand2 << " = " << operand1 * operator2 << endl;
}
else if(operator == '/')
{
if(operand2)
{
cout << opreand1 << " / " << operand2 << " = " << (float)operand1 / (float)operator2 << endl;
}
else
cout << "Warning! Dividing by zero" << endl;
}
return 0;
}
Comments
Leave a comment