C++ Program to Make a Simple Calculator Using Switch Case Statement

In this post, we will learn how to make a simple calculator using switch case statements in C++ Programming language.

This program asks the user to enter two numbers and an arithmetic operator (+, -, *, /). Then, it performs the basic arithmetic operations like Addition, Subtraction, Multiplication, and Division based upon the operator entered by the user..

So, without further ado, let’s begin this tutorial.

C++ Program to Make a Simple Calculator Using Switch Case Statement

// C++ Program to Make a Simple Calculator Using Switch Case Statement
#include <iostream>
using namespace std;

int main(){
    float num1, num2;
    char op;
    
    // Asking for input
    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
    
    cout << "Enter operator: +, -, *, /: ";
    cin >> op;
    
    switch(op){
        case '+':
            cout << num1 << " + " << num2 << " = " << num1 + num2;
            break;
        
        case '-':
            cout << num1 << " - " << num2 << " = " << num1 - num2;
            break;
        
        case '*':
            cout << num1 << " * " << num2 << " = " << num1 * num2;
            break;
        
        case '/':
            cout << num1 << " / " << num2 << " = " << num1 / num2;
            break;
        
        default:
            cout << "Invalid Operator";
            break;
    }
    return 0;
}

Output

Enter the first number: 7
Enter the second number: 5
Enter operator: +, -, *, /: *
7 * 5 = 35

How Does This Program Work ?

    float num1, num2;
    char op;

In this program, we have declared two floating data type variables and one character data type variable named num1, num2, and op respectively.

    // Asking for input
    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
    
    cout << "Enter operator: +, -, *, /: ";
    cin >> op;

Then, the user is asked to enter two numbers and the arithmetic operator. The entered operator gets stored in the op named variable.

    switch(op){
        case '+':
            cout << num1 << " + " << num2 << " = " << num1 + num2;
            break;
        
        case '-':
            cout << num1 << " - " << num2 << " = " << num1 - num2;
            break;
        
        case '*':
            cout << num1 << " * " << num2 << " = " << num1 * num2;
            break;
        
        case '/':
            cout << num1 << " / " << num2 << " = " << num1 / num2;
            break;
        
        default:
            cout << "Invalid Operator";
            break;
    }

If the user enters * operator, then statement for case ‘*’: is executed and the program is executed.

Based on the operator entered by the user, the corresponding operation is performed.

If the entered operator is not defined in the switch case, then the default case will be executed and the program will print ‘Invalid Operator’. 

Conclusion

I hope after going through this post, you understand how to make a simple calculator using switch case statements in C++ Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *