In this C++ program tutorial, we will write a code on how to make a simple calculator using switch statement in C++ or in other words create a simple calculator to add, subtract, multiply and divide using switch and break statement.
In order to understand the program you need to know about the following topics in C++:
C++ Program to Make a Simple Calculator Using Switch Case Statement
# include <iostream>
using namespace std;
int main()
{
char op;
float num1, num2;
cout << "Choose the operator to perform: +, -, *, / : ";
cin >> op;
cout << "\nEnter the First Number: ";
cin >> num1;
cout << "Enter the Second Number: ";
cin >> num2;
switch(op)
{
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// If chose other than the given options
cout << "Error! Operation not supported";
break;
}
return 0;
}
Output:
Choose the operator to perform: +, -, *, / : +
Enter the First Number: 20
Enter the Second Number: 30
20 + 30 = 50
Choose the operator to perform: +, -, *, / : *
Enter the First Number: 5
Enter the Second Number: 12
5 * 12 = 60
Choose the operator to perform: +, -, *, / : &
Enter the First Number: 12
Enter the Second Number: 52
Error! Operation not supported