fork download
  1. #include <stdio.h>
  2.  
  3. void add(double a, double b) {
  4. printf("%.2f + %.2f = %.2f\n", a, b, a + b);
  5. }
  6.  
  7. void subtract(double a, double b) {
  8. printf("%.2f - %.2f = %.2f\n", a, b, a - b);
  9. }
  10.  
  11. void multiply(double a, double b) {
  12. printf("%.2f * %.2f = %.2f\n", a, b, a * b);
  13. }
  14.  
  15. void divide(double a, double b) {
  16. if (b != 0) {
  17. printf("%.2f / %.2f = %.2f\n", a, b, a / b);
  18. } else {
  19. puts("Error: Division by zero.");
  20. }
  21. }
  22.  
  23. int main(void) {
  24. void (*operations[4])(double, double) = {add, subtract, multiply, divide};
  25. int choice;
  26. double n1, n2;
  27.  
  28. printf("Choose: 0 (Add), 1 (Subtract), 2 (Multiply), 3 (Divide): ");
  29. scanf("%d", &choice);
  30.  
  31. if (choice >= 0 && choice <= 3) {
  32. printf("Enter two numbers: ");
  33. scanf("%lf %lf", &n1, &n2);
  34. (*operations[choice])(n1, n2);
  35. } else {
  36. puts("Invalid selection.");
  37. }
  38. return 0;
  39. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Choose: 0 (Add), 1 (Subtract), 2 (Multiply), 3 (Divide): Invalid selection.