fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4.  
  5. // Define the polynomial function
  6. double polynomial(double a, double b, double c, double d, double e, double x)
  7. {
  8. return a * pow(x, 4) + b * pow(x, 3) + c * pow(x, 2) + d * x + e;
  9. }
  10.  
  11. // Bisection method to find the root
  12. double find_root(double a, double b, double c, double d, double e, double p, double q, int n)
  13. {
  14. double epsilon = pow(10, -n); // Precision up to n decimal places
  15. double x, fx;
  16. int iterations = 0;
  17.  
  18. if (polynomial(a, b, c, d, e, p) * polynomial(a, b, c, d, e, q) > 0)
  19. {
  20. printf("The function does not have opposite signs at the interval boundaries.\n");
  21. return 0;
  22. }
  23.  
  24. while (q - p > epsilon)
  25. {
  26. x = (p + q) / 2.0; // Calculate midpoint
  27. fx = polynomial(a, b, c, d, e, x);
  28. iterations++;
  29.  
  30. if (fx * polynomial(a, b, c, d, e, p) > 0)
  31. p = x; // Update lower bound
  32. else
  33. q = x; // Update upper bound
  34. }
  35.  
  36. printf("Root found after %d iterations.\n", iterations);
  37. return x;
  38. }
  39.  
  40. int main()
  41. {
  42. double a, b, c, d, e, p, q, root;
  43. int n;
  44.  
  45. // Input coefficients of the polynomial
  46. printf("Enter coefficients (a, b, c, d, e): ");
  47. scanf("%lf %lf %lf %lf %lf", &a, &b, &c, &d, &e);
  48.  
  49. // Input interval boundaries
  50. printf("Enter interval boundaries (p, q): ");
  51. scanf("%lf %lf", &p, &q);
  52.  
  53. // Input desired precision (number of decimal places)
  54. printf("Enter the number of decimal places for precision: ");
  55. scanf("%d", &n);
  56.  
  57. // Find the root
  58. root = find_root(a, b, c, d, e, p, q, n);
  59.  
  60. if (root != 0)
  61. {
  62. printf("Root = %.*lf\n", n, root); // Display root with n decimal places
  63. }
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0.01s 5304KB
stdin
 
stdout
Enter coefficients (a, b, c, d, e): Enter interval boundaries (p, q): Enter the number of decimal places for precision: Root found after 46 iterations.
Root = 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004669804672305695325280617640132129747945404069810411804768553503652670207097639257830983201840137838993705837755197584383286002536105379742658752687503091080033735321386304968825230804563038634703137192518358833351488184452469625448561782121483600401238816364124477714842354803002354776310371981774772408101974821248382087070202137620039147373475025330846391696259839917302300340321956859949650814736401192100603442720839710223964307985840781525695233320018144769988859024506165125485705927897253850508024571501479122701157784685995708854826470332774189902146287447496849607019196956823015516838968886076008839032946589607988309295990111348268496131761266314409974308372709548617256538583616538070738402049148815465748601355455704720043286215513944625854492187500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000