DEV Community

cloudytech147
cloudytech147

Posted on

C++ Calculator to perform Arithmetic Operations using Switch Case

Through this code model, you will realize "How to Make a Calculator in C++?" This will be a basic calculator, capable of carrying out the four basic arithmetic tasks.

The program begins by inciting the client for choosing an arithmetic activity among the 4 accessible choices. Then, it plays out the activity followed by showing the outcome as the yield of the program. The program exits execution here. Thus, since you get the general idea, here's the actual C++ code:

C++ Calculator to perform Arithmetic Operations using Switch Case

#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();
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
#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;
}
Enter fullscreen mode Exit fullscreen mode

Output

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

Conclusion

That summarizes this showing to perform arithmetic tasks utilizing a switch case in C++. With the previously mentioned code, we have fostered a basic calculator that can perform expansion, subtraction, multiplication, and division.

Note that this is just a single method of composing this program. You can likewise attempt to compose it as you discover best. The more choices you'll investigate, the better you will learn. The very best!

P.S. – Oh indeed, you can likewise parade your code for a similar program by means of the dedicated comments section underneath.

Top comments (0)