Write a c++ program that emulates basics calculator function.
The input should be two int numbers, taken from the user i.e cin.
The output should be number of data type int.
A third decision variable should be of char type.
This should also come from the user.
Use if-block to check the decision char variables and decide upon the mathematical operation(+, -, *,/, %)
Divide by zero should not be allowed for divide and modulus operator.
Check for result for overflow and underflow
#include <iostream>
using namespace std;
int main(){
int num1,num2,result;
char sign;
cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
cout<<"Operation (+,-,*,/): ";
cin>>sign;
cout<<"The result: ";
if(sign=='+') {
if(num1>0&&num2>0&&result<0){
cout<<"overflow";}
else
cout<<result;
}
if(sign=='-'){
result = num1-num2;
if(num2<0&&result>0){
cout<<"underflow";}
else
cout<<result;
}
if(sign=='*'){
result = num1*num2;
if(num1>0&&num2>0&&result<0){
cout<<"overflow";}
else if(result>0&&(num1<0&&num2>0||num2>0&&num1<0)){
cout<<"underflow";
}
else
cout<<result;
}
if(sign=='/'){
if (num2!=0){
result = num1/num2;
cout<<result;
}
else cout<<"can't divide by 0";
}
return 0;
Comments
Leave a comment