From 34389e01685429d9c3f27ccc3c022889cb6dd6bc Mon Sep 17 00:00:00 2001 From: rbiswas-mirp Date: Fri, 2 Oct 2020 17:54:15 +0530 Subject: [PATCH] Create calculator_cpp.cpp --- CPP/calculator_cpp.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 CPP/calculator_cpp.cpp diff --git a/CPP/calculator_cpp.cpp b/CPP/calculator_cpp.cpp new file mode 100644 index 0000000..f9d2074 --- /dev/null +++ b/CPP/calculator_cpp.cpp @@ -0,0 +1,49 @@ +// C++ program to create calculator using +// switch statement +#include +using namespace std; + +// Main program +main() +{ + char op; + float num1, num2; + + // It allows user to enter operator i.e. +, -, *, / + cin >> op; + + // It allow user to enter the operands + cin >> num1 >> num2; + + // Switch statement begins + switch (op) { + + // If user enter + + case '+': + cout << num1 + num2; + break; + + // If user enter - + case '-': + cout << num1 - num2; + break; + + // If user enter * + case '*': + cout << num1 * num2; + break; + + // If user enter / + case '/': + cout << num1 / num2; + break; + + // If the operator is other than +, -, * or /, + // error message will display + default: + cout << "Error! operator is not correct"; + break; + } // switch statement ends + + return 0; +}