Write a program that mimics a calculator. The program should take as input:
It should then output the numbers, the operator, and the result.
Additional notes:
Some sample outputs follow:
3.5 + 4.0 = 7.50
13.973 * 5.65 = 78.95
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
  float first_num, second_num;
  char operation;
  float result;
  Â
  cout<<"Enter the first number: ";
  cin>>first_num;
  Â
  cout<<"Enter the operation to be performed (+,-,/,or *): ";
  cin>>operation;
  Â
  cout<<"Enter the second number: ";
  cin>>second_num;
  Â
  switch(operation)
  {
    case '+':
      result=first_num+second_num;
      cout<<first_num<<operation<<second_num<<"="<<fixed<<setprecision(2)<<result;
      break;
    Â
    case '-':
      result=first_num - second_num;
      cout<<first_num<<operation<<second_num<<"="<<fixed<<setprecision(2)<<result;
      break;
      Â
    case '/':
      if (second_num==0)
      {
        cout<<"Error";
      }
      else
      {
        result=first_num/second_num;
        cout<<first_num<<operation<<second_num<<"="<<fixed<<setprecision(2)<<result; Â
      }
      break;
      Â
    case '*':
      result=first_num * second_num;
      cout<<first_num<<operation<<second_num<<"="<<fixed<<setprecision(2)<<result;
      break;
    Â
    default:
      cout<<"Invalid Operation";
      break;
  }
  Â
  return 0;
}
Comments
Leave a comment