Answer to Question #343490 in C++ for Satt

Question #343490

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

1
Expert's answer
2022-05-22T18:09:56-0400
#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;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS