# C++ program to make a simple calculator

In this program we will learn how to make a simple calculator in c++. The calculator would do four simple operations- addition, subtraction, multiplication and division.

To understand this program, you should have knowledge of the following topics in c++ :

• Variables
• Data types
• Switch statement
• Arithmetic and assignment operators

## Program

``````#include<iostream>
using namespace std;
int main()
{
int num1, num2, result;

char choice;

cout<<"Enter the number";

cin>>num1;

cout<<"Enter the number";

cin>>num2;

cout<<"Enter your choice. + for addition, - for subtraction, * for multiplication, / for division";
cin>>choice;

switch(choice)
{
case '+':
result = num1 + num2;
cout<<"The sum is "<<result;
break;
case '-':
result = num1 - num2;
cout<<"The difference is "<<result;
break;
case '*':
result = num1 * num2;
cout<<"The multiplication is "<<result;
break;
case '/':
result = num1 / num2;
cout<<"The division is "<<result;
break;
default:
cout<<"Invalid choice";
}
return 0;

}``````

## Output

`Enter first number12`

`Enter second number2`

`Enter the choice. + for addition, - for subtraction, * for multiplication, / for division+`

`The sum is 14`

### Explanation :

• First of all we have declared three integer type variables num1, num2 and result and a character type variable named choice.
• Then we are asking the user to enter two numbers individually and storing them in num1 and num2.
• After that we a re asking the user to enter the choice. + for addition, – for subtraction, * for multiplication, / for division.
• We are using the switch statement to calculate the result depending upon the choice the user enters. There are four individual cases for addition, subtraction, multiplication and division. The result is calculated depending upon the choice. For example if the user give choice +, the addition of the two numbers would be calculated and displayed.
• The default case would be executed in case if the user enters an invalid character instead.

Note : Likewise other conditional statements, in switch statement, only one out of every cases would execute at a time. The break statement inside any case terminate the switch.