Algorithm
1. Start
2. Declare variables:
- num1, num2 (for operands)
- choice (for operation selection)
- result (for storing the result)
3. Display menu to the user:
- Addition
- Subtraction
- Multiplication
- Division
4. Take user input for choice.
5. Use a switch-case statement based on the user's choice:
a. Case 1: Addition
- Take user input for num1 and num2.
- Add num1 and num2 and store the result.
b. Case 2: Subtraction
- Take user input for num1 and num2.
- Subtract num2 from num1 and store the result.
c. Case 3: Multiplication
- Take user input for num1 and num2.
- Multiply num1 and num2 and store the result.
d. Case 4: Division
- Take user input for num1 and num2.
- Check if num2 is not equal to 0.
- If true, divide num1 by num2 and store the result.
- If false, display an error message (division by zero).
e. Default:
- Display an error message for an invalid choice.
6. Display the result if the operation was successful.
7. End
Code Examples
#1 Code Example-C++ Program to Make a Simple Calculator Using Switch Case Statement
Code -
C++ Programming
// C++ Program to Make a Simple Calculator Using Switch Case Statement
#include <iostream>
using namespace std;
int main(){
float num1, num2;
char op;
// Asking for input
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter operator: +, -, *, /: ";
cin >> op;
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:
cout << "Invalid Operator";
break;
}
return 0;
}
Copy The Code &
Try With Live Editor
Output
Enter the second number: 5
Enter operator: +, -, *, /: *
7 * 5 = 35
#2 Variable Declaration:'num1' and 'num2' are variables to store the two numbers entered by the user. 'op' is a variable to store the operator entered by the user.
Code -
C++ Programming
float num1, num2;
char op;
Copy The Code &
Try With Live Editor
#3 User Input: The program prompts the user to enter two numbers and an operator. The values are then stored in num1, num2, and op, respectively.
Code -
C++ Programming
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter operator: +, -, *, /: ";
cin >> op;
Copy The Code &
Try With Live Editor
#4 Switch Case: The switch statement checks the value of the op variable.
Code -
C++ Programming
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:
cout << "Invalid Operator";
break;
}
Copy The Code &
Try With Live Editor
#5 Return Statement: The program ends with a return statement indicating successful execution
Code -
C++ Programming
return 0;
Copy The Code &
Try With Live Editor
Demonstration
C++ Programing Example to Make a Simple Calculator Using Switch Case Statement-DevsEnv