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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # 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