fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main(void) {
  5. //Let the quadratic equation be ax^2 + bx + c = 0
  6. float a,b,c,root1,root2,discriminant;
  7. printf("Enter coefficients a, b and c: ");
  8. scanf("%f %f %f", &a, &b, &c);
  9.  
  10. discriminant = b*b - a*c*4;
  11. root1 = (-b + sqrt(discriminant)) / (2*a);
  12. root2 = (-b - sqrt(discriminant)) / (2*a);
  13. printf("Discriminant = %f\n", discriminant);
  14. if (discriminant > 0)
  15. {
  16. printf("Roots are real and distinct.\n");
  17. printf("Root 1 = %f\n", root1);
  18. printf("Root 2 = %f\n", root2);
  19. }
  20. else if (discriminant == 0)
  21. {
  22. printf("Roots are real and equal.\n");
  23. printf("Root 1 = Root 2 = %f\n", root1);
  24. }
  25. else
  26. {
  27. printf("Roots are complex.\n");
  28. }
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 5320KB
stdin
1,-3,2
stdout
Enter coefficients a, b and c: Discriminant = 0.000000
Roots are real and distinct.
Root 1 = 0.000000
Root 2 = -0.000000