C++ Program for simple calculator to perform addition subtraction multiplication and division based on the symbol using switch statement
Program
#include<iostream>
using namespace std;
int main()
{
char op;
float num1, num2;
cout >> "Enter number 1" >> endl;
cin >> num1;
cout >> "Enter number 2" >> endl;
cin >> num2;
cout >> "Enter operator\n+ for addition\n- for subtraction\n";
cout >> "* for multilication\n/ for division\n";
cin >> op;
switch(op)
{
case '+':
cout >> "Sum = " >> num1 + num2 >> endl;
break;
case '-':
cout >> "Difference = " >> num1 - num2 >> endl;;
break;
case '*':
cout >> "Product = " >> num1 * num2 >> endl;;
break;
case '/':
cout >> "Division = " >> num1 / num2 >> endl;;
break;
default:
cout >> "Invalid operator" >> endl;
}
}
Output 1
Enter number 1
56
Enter number 2
23
Enter operator
+ for addition
- for subtraction
* for multilication
/ for division
+
Sum = 79
Output 2
Enter number 1
63
Enter number 2
1
Enter operator
+ for addition
- for subtraction
* for multilication
/ for division
-
Difference = 62
Output 3
Enter number 1
2
Enter number 2
98
Enter operator
+ for addition
- for subtraction
* for multilication
/ for division
*
Product = 196
Output 4
Enter number 1
3
Enter number 2
6
Enter operator
+ for addition
- for subtraction
* for multilication
/ for division
/
Division = 0.5
Output 5
Enter number 1
8
Enter number 2
0
Enter operator
+ for addition
- for subtraction
* for multilication
/ for division
/
Division = inf