Write a program that works as a simple calculator.
1.It reads a character (ch)
2.If ch is among β+β, β-β, β*β, β/β or β%β it furthur takes two numbers (N1 and N2 as input). It then performs appropriate appropriate operation between numbers and print the number. 3.If ch is βXβ or βxβ, the program terminates.
4.If ch is any other character, the program should print βInvalid operation. Try again.β and seek inputs again (starting from character).
Write code to achieve above functionality.
include
using namespace std;
int main()
{
long long int a,b,c;
char ch;
cin>>ch;
while(ch!='+' && ch!='X')
{
if(ch=='+')
{
cin>>a>>b;
c=a+b;
cout<<c<<endl;
}
else if(ch=='-')
{
cin>>a>>b;
c=a-b;
cout<<c<<endl;
}
else if(ch=='*')
{
cin>>a>>b;
c=a*b;
cout<<c<<endl;
}
else if(ch=='%' && b!=0)
{
cin>>a>>b;
c=a%b;
cout<<c<<endl;
}
else if(ch=='/' && b!=0)
{
cin>>a>>b;
c=a/b;
cout<<c<<endl;
}
else
cout<<"Invalid operation. Try again."<<endl;
break;
cin>>ch;
}
return 0;
}