04_quadsolve.c 620 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <math.h>
  2. #include <stdio.h>
  3. int solvequadeq(double a, double b, double c, double *x1, double *x2)
  4. {
  5. double D = pow(b, 2) - 4 * a * c;
  6. if (D < 0)
  7. return 0;
  8. if (D == 0)
  9. {
  10. *x1 = (-b) / (2 * a);
  11. return 1;
  12. }
  13. else
  14. {
  15. *x1 = (-b - sqrt(D)) / (2 * a);
  16. *x2 = (-b + sqrt(D)) / (2 * a);
  17. return 2;
  18. }
  19. }
  20. int main()
  21. {
  22. double a = 1;
  23. double b = -4;
  24. double c = 3;
  25. printf("Equation: %f*x^2 + %f*x + %f = 0\n", a, b, c);
  26. double x1, x2;
  27. int r = solvequadeq(a, b, c, &x1, &x2);
  28. printf("%d solution(s) found\n", r);
  29. if (r > 0)
  30. printf("x1 = %f\n", x1);
  31. if (r > 1)
  32. printf("x2 = %f\n", x2);
  33. return 0;
  34. }