Q. C++ program to make a simple calculator using switch case.
Here you will find an algorithm and program in C++ programming language to make a simple calculator. First let us understand what is calculator?
Explanation : A calculator is used to make mathematical calculations. In this program we will create a simple calculator that can perform an arithmetic operation (+, -, *, /).
Calculator Program Algorithm
START Step 1 : Initialise the two numbers. Step 2 : Ask the user to enter an option by giving six options. Step 3 : After getting the option from the user write if conditions for every operation based on the option. Step 4 : Perform the respective operation. Step 5 : Print the result. STOP
C++ Program to Make a Simple Calculator
#include <bits/stdc++.h> using namespace std; int main() { char ch; float num1, num2; cout << "Enter an operator (+, -, *, /): "; cin >> ch; cout << "Enter two operands: "; cin >> num1 >> num2; switch(ch) { case '+': cout << num1+num2; break; case '-': cout << num1-num2; break; case '*': cout << num1*num2; break; case '/': cout << num1/num2; break; default: // If the operator is other than +, -, * or /, error message is shown cout << "Error! operator is not correct"; break; } return 0; }
Output
Enter an operator (+, -, *, /): * Enter two operands: 8 6 8.0 * 6.0 = 48.0