C++ Calculator to perform Arithmetic Operations using Switch Case
Tags: cpp, algorithms, switchcase
Published: 2021-09-21 12:45:59

Through this code model, you will realize "[How to Make a Calculator in C++](https://www.techgeekbuzz.com/write-a-cpp-program-to-perform-arithmetic-operations-using-switch-case/)?" 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](https://secureservercdn.net/160.153.137.163/84g.4be.myftpupload.com/wp-content/uploads/2019/07/Write-a-C-Program-to-perform-arithmetic-operations-using-switch-case-768x384.png)

```
#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
```

```
#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
```

## 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.