# Program to use a Basic Calculator using Switch Case in C++

Numbers are a very important part of the world and its functioning. Each measurement needs numerical value and calculation of anything is vital for exact functioning.

```#include<iostream.h>
#include<conio.h>
```

The libraries used in this program are <iostream.h> and <conio.h>.

## The main() program: Basic Calculator using Switch Case in C++

Let’s divide the program into 2 parts to understand better

### Part 1

```void main()
{
clrscr();
float a,b,c,d,n1,n2;
int func;
cout<<"Enter the number for the operation to be performed";
cout<<"'1' for addition\n'2' for subtraction\n'3' for multiplication\n'4' for division\n";
cin>>func;
cout<<"Enter the first number to perform the operation";
cin>>n1;
cout<<"Enter the second number to perform the operation";
cin>>n2;
```

In this program, multiple functions can be created for different operations. But to keep the program simple, a single function is used.

Starting with the main() function, first clrscr() function is used to clear the console screen. Four float variables are initialized to store the result of the operation performed. ‘n1’ and ‘n2’ are the variables which store the number upon whom the operation is to be performed. Initialize a variable to store an integer value to define the operation to be performed. Print command to enter the value of the integer variable ‘func’. Let ‘1’ be the case of adding two variables, ‘2’ be the case of subtracting second variable from the first and use ‘3’ and ‘4’ for multiplication and division respectively. Take in input of the values on which the operations are to be performed.

### Part 2

```switch(func)
{
case(1):a=n1+n2;
cout<<n1<<"+"<<n2<<"="<<a;
break;
case(2):b=n1-n2;
cout<<n1<<"-"<<n2<<"="<<b;
break;
case(3):c=n1*n2;
cout<<n1<<"*"<<n2<<"="<<c;
break;
case(4):d=n1/n2;
cout<<n1<<"/"<<n2<<"="<<d;
break;
default:cout<<"Invalid Input";
}
getch();
}```

Here, switch case will be used to perform different operation based upon the value of the user entered variable ‘func’. For example, if the value of ‘func’ is ‘1’, then the addition operation for ‘n1’ and ‘n2’ variable will be performed and stored in ‘a’. Similarly, different operation will be performed for ‘2’, ‘3’ and ‘4’. If the user enters a value other than these, the default case will be executed. Here, the default case states that the input is invalid. Each of these cases are separated by the ‘break’ function. The break function, disables the execution of the upcoming cases.

Further, getch() is used to hold on the console screen till the inputs and outputs are performed.

#### Output

```Enter the number for the operation to be performed
3
Enter the first number to perform the operation
4
Enter the second number to perform the operation
2
4*2=8```