Write a program to be used as a calculator. It should be able to add(+), subtrac
ID: 3628550 • Letter: W
Question
Write a program to be used as a calculator. It should be able to add(+), subtract(-),multiply(*),divide(/),to find remainder of integer division(%),exponent(e),and square root (s) of numbers.The user enters inputs in the form:operator,number,(number);where the second number is not required for the exponent or square root. For example, if the user enters +5 4,then the program prints out 9; or if the user enters s 64, the program prints out 8.
Test your program for a wide range of possible inputs including division by zero and square root of negative values.
submit the source code and properly formatted output.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
char oper;
int var1 = 0, var2 = 0;
cout<<"enter operation variable1 variable 2 (ex + 1 2) q to exit ";
cin>>oper;
while(oper!='q')
{cin>>var1;
if(oper!='e'&&oper!='s')
{ cin>>var2;
cout<<var1<<" "<<oper<<" "<<var2<<" = ";
}
else
cout<<oper<<" "<<var1<<" = ";
switch(oper)
{case '+':cout<<var1+var2;
break;
case '-':cout<<var1-var2;
break;
case '*':cout<<var1*var2;
break;
case '/':if(var2==0)
cout<<"undefined";
else
cout<<(double)var1/var2;
break;
case '%':if(var2==0)
cout<<"undefined";
else
cout<<var1%var2;
break;
case 'e':cout<<exp(var1);
break;
case 's': if(var1<0)
cout<<"answer is imaginary ";
else
cout<<sqrt(var1);
break;
default: cout<<"Invalid operator";
}
cout<<endl;
cout<<"enter operation variable1 variable 2 (ex + 1 2) q to exit ";
cin>>oper;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.