Write C++ program to create simple calculator using switch Statement

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
 
using namespace std;
 
int main(){
    char mathoperator;
    double firstNumber, secondNumber;
 
    //Reading mathoperator from user
    cout<<"Enter an operator (+, -, *,): ";
    cin>>mathoperator;
 
   //Reading operands from user
    cout<<"Enter two operands: ";
    cin>>firstNumber;
    cin>>secondNumber;
 
    switch(mathoperator)
    {
        cout << firstNumber << " + " << secondNumber << " = " << firstNumber+secondNumber;
            break;
 
        case '-':
             cout << firstNumber << " - " << secondNumber << " = " << firstNumber-secondNumber;
            break;
 
        case '*':
             cout << firstNumber << " * " << secondNumber << " = " << firstNumber*secondNumber;
            break;
 
        case '/':
             cout << firstNumber << " / " << secondNumber << " = " << firstNumber/secondNumber;
            break;
 
        // operator doesn't match any case constant (+, -, *, /)
        default:
            cout<<"Error! please enter correct operator";
    }
 
    return 0;
 
}

Result

Write C++ program to create simple calculator using switch Statement
Write C++ program to create simple calculator using switch Statement

Leave a Comment