Created
January 16, 2023 12:19
-
-
Save coder-ralph/3232d30483d936e7b56c74e0ad7541fe to your computer and use it in GitHub Desktop.
Basic calculator using switch cases in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <iostream> | |
using namespace std; | |
int main() { | |
char op; | |
float num1, num2; | |
cout << "Enter operator: +, -, *, /: "; | |
cin >> op; | |
cout << "Enter two operands: "; | |
cin >> num1 >> 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: | |
cout << "Error! operator is invalid"; | |
break; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment