Write C++ program to create simple calculator using switch Statement


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6. char mathoperator;
  7. double firstNumber, secondNumber;
  8.  
  9. //Reading mathoperator from user
  10. cout<<"Enter an operator (+, -, *,): ";
  11. cin>>mathoperator;
  12.  
  13. //Reading operands from user
  14. cout<<"Enter two operands: ";
  15. cin>>firstNumber;
  16. cin>>secondNumber;
  17.  
  18. switch(mathoperator)
  19. {
  20. cout << firstNumber << " + " << secondNumber << " = " << firstNumber+secondNumber;
  21. break;
  22.  
  23. case '-':
  24. cout << firstNumber << " - " << secondNumber << " = " << firstNumber-secondNumber;
  25. break;
  26.  
  27. case '*':
  28. cout << firstNumber << " * " << secondNumber << " = " << firstNumber*secondNumber;
  29. break;
  30.  
  31. case '/':
  32. cout << firstNumber << " / " << secondNumber << " = " << firstNumber/secondNumber;
  33. break;
  34.  
  35. // operator doesn't match any case constant (+, -, *, /)
  36. default:
  37. cout<<"Error! please enter correct operator";
  38. }
  39.  
  40. return 0;
  41.  
  42. }
  43.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts