fork download
  1. #include <stdio.h>
  2.  
  3. // ฟังก์ชันบวก
  4. int add(int a, int b) {
  5. return a + b;
  6. }
  7.  
  8. // ฟังก์ชันลบ
  9. int subtract(int a, int b) {
  10. return a - b;
  11. }
  12.  
  13. // ฟังก์ชันคูณ
  14. int multiply(int a, int b) {
  15. return a * b;
  16. }
  17.  
  18. // ฟังก์ชันหาร
  19. float divide(int a, int b) {
  20. if (b != 0) {
  21. return (float)a / b;
  22. } else {
  23. printf("Error: Division by zero\n");
  24. return 0;
  25. }
  26. }
  27.  
  28. // ฟังก์ชันอินทิเกรต
  29. double integrate(double a, double b, double (*f)(double)) {
  30. double area = 0;
  31. double dx = 0.0001; // ความกว้างของแท่งสำหรับการแยกพื้นที่
  32. for (double x = a; x < b; x += dx) {
  33. area += f(x) * dx;
  34. }
  35. return area;
  36. }
  37.  
  38. // ฟังก์ชันดิฟ
  39. double differentiate(double x, double (*f)(double)) {
  40. double dx = 0.0001;
  41. double fx_plus_dx = f(x + dx);
  42. double fx_minus_dx = f(x - dx);
  43. return (fx_plus_dx - fx_minus_dx) / (2 * dx);
  44. }
  45.  
  46. // ฟังก์ชัน f(x) = x^2
  47. double square(double x) {
  48. return x * x;
  49. }
  50.  
  51. int main() {
  52. int num1, num2;
  53. printf("Enter two numbers: ");
  54. scanf("%d %d", &num1, &num2);
  55.  
  56. printf("Addition: %d\n", add(num1, num2));
  57. printf("Subtraction: %d\n", subtract(num1, num2));
  58. printf("Multiplication: %d\n", multiply(num1, num2));
  59. printf("Division: %.2f\n", divide(num1, num2));
  60.  
  61. double a, b;
  62. printf("Enter integration bounds (a b): ");
  63. scanf("%lf %lf", &a, &b);
  64. printf("Integration of x^2 from %.2f to %.2f: %.2f\n", a, b, integrate(a, b, square));
  65.  
  66. double point;
  67. printf("Enter a point for differentiation: ");
  68. scanf("%lf", &point);
  69. printf("Differentiation at %.2f: %.2f\n", point, differentiate(point, square));
  70.  
  71. return 0;
  72. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Enter two numbers: Addition: 0
Subtraction: 0
Multiplication: 0
Error: Division by zero
Division: 0.00
Enter integration bounds (a b): Integration of x^2 from 0.00 to 0.00: 0.00
Enter a point for differentiation: Differentiation at 0.00: 0.00