Write a program that mimics a calculator. The program should take as input:
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
#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!!!";
  }
}
Comments
Leave a comment