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 number ,the operator and result
#include <iostream>
using namespace std;
int main(){
int x, y;
char c;
cout<<"Input expression, eg 4 + 5\n";
cin>>x;
cin>>c;
cin>>y;
cout<<x<<" "<<c<<" "<<y<<" = ";
switch(c){
case '+': cout<<x + y; break;
case '-': cout<<x - y; break;
case '*': cout<<x * y; break;
case '/': cout<<x / y; break;
case '%': cout<<x % y; break;
default: cout<<"Invalid operator!";
}
return 0;
}
Comments
Leave a comment