fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. char operator;
  5. double num1, num2;
  6.  
  7. // รับค่าตัวเลขและตัวดำเนินการจากผู้ใช้
  8. printf("Enter first number: ");
  9. scanf("%lf", &num1);
  10.  
  11. printf("Enter an operator (+, -, *, /): ");
  12. scanf(" %c", &operator);
  13.  
  14. printf("Enter second number: ");
  15. scanf("%lf", &num2);
  16.  
  17. // คำนวณตามตัวดำเนินการที่ผู้ใช้ใส่
  18. switch (operator) {
  19. case '+':
  20. printf("Result: %.2f\n", num1 + num2);
  21. break;
  22. case '-':
  23. printf("Result: %.2f\n", num1 - num2);
  24. break;
  25. case '*':
  26. printf("Result: %.2f\n", num1 * num2);
  27. break;
  28. case '/':
  29. if (num2 != 0) {
  30. printf("Result: %.2f\n", num1 / num2);
  31. } else {
  32. printf("Error: Division by zero is not allowed.\n");
  33. }
  34. break;
  35. default:
  36. printf("Error: Invalid operator.\n");
  37. break;
  38. }
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Enter first number: Enter an operator (+, -, *, /): Enter second number: Error: Invalid operator.