fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef struct Q {
  5.  
  6. float a,
  7. b,
  8. c;
  9.  
  10. } E;
  11.  
  12. float computeDelta(float a, float b, float c) {
  13.  
  14. return b*b - 4*a*c;
  15. }
  16.  
  17. void displayEq(float a, float b, float c) {
  18.  
  19. printf("%3.3f*x^2 + %3.3f*x + %3.3f = 0\n", a, b, c);
  20. }
  21.  
  22. int main() {
  23.  
  24. E q;
  25. float D;
  26. printf("a,b,c=?");
  27. scanf("%f %f %f", &q.a, &q.b, &q.c);
  28.  
  29. D = computeDelta(q.a,q.b,q.c);
  30.  
  31. displayEq(q.a, q.b, q.c);
  32.  
  33. if(D < 0) printf("%s\n", "Equation do not have real solutions.");
  34.  
  35. else if(D == 0) printf("x1 = x2 = %3.3f\n", -q.b/2*q.a);
  36.  
  37. else if(D > 0) {
  38.  
  39. float x1, x2;
  40. x1 = (-q.b + sqrt(D))/(2*q.a);
  41. x2 = (-q.b - sqrt(D))/(2*q.a);
  42. printf("x1 = %3.3f x2 =%3.3f\n", x1, x2);
  43. }
  44.  
  45. return(0);
  46. }
Success #stdin #stdout 0s 4488KB
stdin
2.340 4.670 0.320
stdout
a,b,c=?2.340*x^2 + 4.670*x +  0.320 = 0
x1 = -0.071 x2 =-1.925