Answer to Question #183004 in C++ for Vincenzo Santoro

Question #183004

Write a program that mimics a calculator. The program should take as input:

  1. The first integer
  2. The second integer
  3. The operation to be performed (+, -, *, /)

It should then output the numbers, the operator, and the result. (For division, if the denominator is zero, output an appropriate message. The message should contain the word "error")

Some sample outputs follow:

3 + 4 = 7
13 * 5 = 65
1
Expert's answer
2021-04-24T15:12:44-0400
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,n,add,sub,multiply;
    float divide;
    cout<<"Enter first integer ";
    cin>>a;
    cout<<"\nEnter second integer ";
    cin>>b;
    cout<<"\nEnter the operation to be performed\n1. Addition\n2. Subtraction\n3. Division\n4. Multiplication\n";
    cin>>n;
    switch(n)
    {
        case 1: add=a+b;
                cout<<a<<" + "<<b<<" = "<<add;
                break;
        case 2: sub=a-b;
                cout<<a<<" - "<<b<<" = "<<sub;
                break;
        case 3: if(b==0)
                {
                    cout<<"error";
                }
                else
                {
                    divide=a/b;
                    cout<<a<<" / "<<b<<" = "<<divide;
                }
                break;
        case 4: multiply=a*b;
                cout<<a<<" * "<<b<<" = "<<multiply;
                break;
        default: cout<<"\nInvalid input!!!";
    }
}

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