YouTip LogoYouTip

Cpp Examples Calculator Switch Case

# C++ Example - Implementing a Simple Calculator [![Image 3: C++ Examples](#) C++ Examples](#) Create a simple calculator using C++ that can perform +, -, *, /. ## Example #includeusing namespace std; int main(){char op; float num1, num2; cout<>op; cout<>num1>>num2; switch(op){case'+': cout<<num1+num2; break; case'-': cout<<num1-num2; break; case'*': cout<<num1*num2; break; case'/': if(num2 == 0){cout<<"error cannot divide by zero"; break; }else{cout<<num1 / num2; break; }default: // If the operator is not +, -, * or /, display an error message cout<<"Error! Please enter a correct operator."; break; }return 0; } The output of the above program execution is: Enter operator: +, -, *, / : +Enter two numbers: 123 ## Example 2 #includeusing namespace std; double add(double num1, double num2){return num1 + num2; }double subtract(double num1, double num2){return num1 - num2; }double multiply(double num1, double num2){return num1 * num2; }double divide(double num1, double num2){if(num2 != 0){return num1 / num2; }else{cou
← Md TutorialCpp Examples Hcf Gcd β†’