
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Make a Simple Calculator Using Switch Case
In this article, we'll show you how to write a C++ program to create a simple calculator that can add, subtract, multiply, or divide using a switch statement. The calculator works by taking two numbers(operands) and an operator (+, -, *, /) from the user, and then it performs the chosen operation.
Lets understand it better with an example:
Suppose the user enters: a = 5 and b = 4 Then, based on the chosen operator: If the operator is +, the result is 5 + 4 = 9 If the operator is -, the result is 5 - 4 = 1 If the operator is *, the result is 5 * 4 = 20 If the operator is /, the result is 5 / 4 = 1.25
Using Switch Statement for Calculator Operations
We use a switch statement to perform calculator operations. A switch statement is a control structure in C++ that compares a variable against multiple values and runs the matching block of code. Here are the steps we follow:
- First, we ask the user to enter two numbers.
- Then, we ask which operation they want to perform.
- Based on the input symbol (+, -, *, /), we run the corresponding case using the switch statement.
- If the operator doesn't match any of the cases, we display an error message.
Example
Here's a complete C++ program to make a simple calculator that can add, subtract, multiply, or divide using the switch statement.
#include <iostream> using namespace std; int main() { double first, second; // To store user numbers char symbol; // To store the math operator // Ask for the operator cout << "Enter an operator (+, -, *, /): "; cin >> symbol; // Ask for the first number cout << "Enter the first number: "; cin >> first; // Ask for the second number cout << "Enter the second number: "; cin >> second; // Do the calculation based on the operator switch(symbol) { case '+': cout << "Result: " << first + second << endl; break; case '-': cout << "Result: " << first - second << endl; break; case '*': cout << "Result: " << first * second << endl; break; case '/': if (second != 0) cout << "Result: " << first / second << endl; else cout << "Error: Cannot divide by zero!" << endl; break; default: cout << "Error: Invalid operator!" << endl; } return 0; }
The output below shows the result when the user selects '+' and enters 34 and 76, which gives an output of 110.
Enter an operator (+, -, *, /): + Enter the first number: 34 Enter the second number: 76 Result: 110
Here, the user selects '/' and enters 98 and 4. The output is displayed below.
Enter an operator (+, -, *, /): / Enter the first number: 98 Enter the second number: 4 Result: 24.5
Time Complexity: O(1) because the program performs a constant number of operations.
Space Complexity: O(1) because the program uses a constant amount of memory.