Write a C++ Calculator to perform Arithmetic Operations using Switch Case

Posted in

Write a C++ Calculator to perform Arithmetic Operations using Switch Case

Vinay Khatri
Last updated on November 27, 2022

    Programming is learned best by practice i.e. by writing code. There are several basic C++ programs that you need to complete first in order to advance with the high-level, object-oriented programming language. In this sample C++ program, we will demonstrate using the C++ switch case to perform arithmetic operations i.e.

    • Addition,
    • Subtraction,
    • Multiplication, and
    • Division.

    So, are you ready? Here we go!

    A Simple C++ Program to Perform Arithmetic Operations using the Switch Case

    Through this code example, you will learn "How to Make a Calculator in C++?" This will be a basic calculator, capable of carrying out the four basic arithmetic operations. The program starts by prompting the user for choosing an arithmetic operation among the 4 available options. Next, it performs the operation followed by displaying the result as the output of the program. The program exits execution here. So, now that you get the gist of it, here's the actual C++ code:

    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    float num_1,num_2;
    int operation;
    
    cout<<"What Arithmetic Operation do you want to perform:\n";
    cout<<"Press 1 for Addition \n" ;
    cout<<"Press 2 for Subtraction\n";
    cout<<"Press 3 for Multiplication\n";
    cout<<"Press 4 for Division\n";
    cin>>operation;
    
    cout<<"Now Enter Two Numbers\n";
    cin>>num_1>>num_2;
    
    switch (operation)
    
        {
        case 1:
        cout<<"The Addition result is: "<<num_1+num_2;
        break;
    
        case 2:
        cout<<"The Subtraction result is: "<<num_1-num_2;
        break;
    
        case 3:
        cout<<"The Multiplication result is: "<<num_1*num_2;
        break;
    
        case 4:
        cout<<"The Division result is: "<<num_1/num_2;
        break;
        }
    getch();
    }

    Sample Output:

    What Arithmetic Operation do you want to perform:
    
    
    Press 1 for Addition
    Press 2 for Subtraction
    Press 3 for Multiplication
    Press 4 for Division
    
    4
    
    Now Enter Two Numbers
    
    100
    30
    
    The Division result is: 3.33333

    A Simple C Program to Perform Arithmetic Operations using the Switch Case

    #include <stdio.h>
    
    int main() 
    {
        char operator;
        double num1, num2;
    	//ask user to enter the operator
        printf("Select the operator (+, -, *, /): ");
        scanf("%c", &operator);
        
        //ask user to enter two numbers
        printf("Enter two numbers (eg. 12 3): ");
        scanf("%lf %lf",&num1, &num2);
    	
    	//start switch case
        switch(operator)
        {	//if operator is addition
            case '+':
                printf("%.1lf + %.1lf = %.1lf",num1, num2, num1+num2);
                break;
    
    		// if operator is substraction
            case '-':
                printf("%.1lf - %.1lf = %.1lf",num1, num2, num1-num2);
                break;
                
    		//if operator is multiplication
            case '*':
                printf("%.1lf * %.1lf = %.1lf",num1, num2, num1*num2);
                break;
    	
    		//if operator is division
            case '/':
                printf("%.1lf / %.1lf = %.1lf",num1, num2, num1/num2);
                break;
    
            // if user enter a wrong operaotr
            default:
                printf("Please select the correct operator");
        }
    
        return 0;
    }

    Output

    Select the operator (+, -, *, /): *
    Enter two numbers (eg. 12 3): 4 5
    4.0 * 5.0 = 20.0

    C++ program for arithmetic operations using function

    The above example is not modular, we have implemented all the logic in the main program. We can also implement the same C++ program for addition, subtraction, multiplication, and division using a switch case and wrap it inside a function. You would ask why to use the function, the answer is simple, functions make the code modular, and easy to read. For this particular example, the function implementation won't bring much change to the program's legibility. Still, if you are learning function , you can write a C++ program for arithmetic operations using function.

    For example, write a program to perform arithmetic operations in c++

    Now let's write a menu-driven program to perform arithmetic operations in C++ using function and switch case.

    #include <iostream>
    using namespace std;
    
    // function to perfrom C++ operation
    void arithmetic(float num_1, float num_2)
    {
        int operation;
        cout<<"What Arithmetic Operation do you want to perform:\n";
        cout<<"Press 1 for Addition \n" ;
        cout<<"Press 2 for Subtraction\n";
        cout<<"Press 3 for Multiplication\n";
        cout<<"Press 4 for Division\n"; cin>>operation;
        
        switch (operation)
        
            {
            case 1:
            cout<<"The Addition result is: "<<num_1+num_2;
            break;
        
            case 2:
            cout<<"The Subtraction result is: "<<num_1-num_2;
            break;
        
            case 3:
            cout<<"The Multiplication result is: "<<num_1*num_2;
            break;
        
            case 4:
            cout<<"The Division result is: "<<num_1/num_2;
            break;
            }
    }
    
    int main() {
        
        float num_1, num_2;
        cout<<"Now Enter Two Numbers\n"; cin>>num_1>>num_2;
        
        // call the function
        arithmetic(num_1, num_2);
    
        return 0;
    }

    Output

    Now Enter Two Numbers
    20 40
    What Arithmetic Operation do you want to perform:
    Press 1 for Addition 
    Press 2 for Subtraction
    Press 3 for Multiplication
    Press 4 for Division
    1
    The Addition result is: 60

    Conclusion

    That sums up this demonstration to perform arithmetic operations using a switch case in C++. With the above-mentioned code, we have developed a basic calculator that can perform addition, subtraction, multiplication, and division.

    In this article, we have covered two different ways to implement a C++ program for arithmetic operation using a switch case. Note that this is only one way of writing this program. You can also try to write it as you find best. The more options you'll explore, the better you will learn.

    All the best!

    P.S. - Oh yes, you can also flaunt your code for the same program via the dedicated comments section below. Do you wish to master the C++ language? If yes, sign up for this course now!

    People are also reading:

    Leave a Comment on this Post

    0 Comments