# C++ program to make simple calculator using switch case

In this example, you will learn about C++ program to make simple calculator using switch case i.e. Addition, Subtraction, Multiplication, Squares, and Division.

This program uses 6 different cases for performing the different mathematical operation. You have to select different options for addition, subtraction, multiplication, square and division.

## C++ program to make simple calculator using switch case

``````/* C++ program to implement simple functions of a calculator*/

#include<iostream>
using namespace std;

int main()
{
int choice;
long num1, num2, x;

//displaying different options
"\n2 = Subtraction"
"\n3 = Multiplication"
"\n4 = Division"
"\n5 = Squares"
"\n6 = exit"
"\n\nChoice: ";
cin >> choice;

//while loop check whether the choice is in the given range
while(choice < 1 || choice > 6)
{
cout << "\nPlease choose the above mentioned option."
"\nChoice: ";
cin >> choice;
}

switch (choice)
{
case 1:
cout << "Enter two numbers: \n";
cin >> num1 >> num2;
x = num1 + num2;
cout << "Sum = " << x;
break;

//Subtraction
case 2:
cout << "Enter two numbers: \n";
cin >> num1 >> num2;
x = num1 - num2;
cout << "Subtraction = " << x;
break;

//Multiplication
case 3:
cout << "Enter two numbers: \n";
cin >> num1 >> num2;
x = num1 * num2;
cout << "Product = " << x;
break;

//Division
case 4:
cout << "Enter Dividend: ";
cin >> num1;
cout << "Enter Divisor: ";
cin >> num2;

//while loop checks for divisor whether it is zero or not
while(num2 == 0)
{
cout << "\nDivisor cannot be zero."
"\nEnter divisor once again: ";
cin >> num2;
}
x = num1 / num2;
cout << "\nQuotient = " << x;
break;

//Square
case 5:
cout << "Enter any number: \n";
cin >> num1;
x = num1 * num1;
cout << "Square = " << x;
break;

case 6:
return 0;

default: cout << "\nError";
}
}
``````

Output Explanation

In the above example, we have made a simple calculator in C++ with basic functions.

In a division, there is a special case to check whether divisor is zero.