fork download
  1. // Program to create a simple calculator
  2. // Performs addition, subtraction, multiplication or division depending the input from user
  3.  
  4. # include <stdio.h>
  5.  
  6. int main() {
  7.  
  8. char operator;
  9. double firstNumber,secondNumber;
  10.  
  11. printf("Enter an operator (+, -, *, /): ");
  12. scanf("%c", &operator);
  13.  
  14. printf("Enter two operands: ");
  15. scanf("%lf %lf",&firstNumber, &secondNumber);
  16.  
  17. switch(operator)
  18. {
  19. case '+':
  20. printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
  21. break;
  22.  
  23. case '-':
  24. printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
  25. break;
  26.  
  27. case '*':
  28. printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
  29. break;
  30.  
  31. case '/':
  32. printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
  33. break;
  34.  
  35. // operator is doesn't match any case constant (+, -, *, /)
  36. default:
  37. printf("Error! operator is not correct");
  38. }
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 9432KB
stdin
+
50 60
stdout
Enter an operator (+, -, *, /): Enter two operands: 50.0 + 60.0 = 110.0