fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main() {
  5. float a, b, c, discriminant, root1, root2;
  6.  
  7. // รับค่า a, b, และ c จากผู้ใช้
  8. printf("Enter coefficients a, b, and c: ");
  9. scanf("%f %f %f", &a, &b, &c);
  10.  
  11. // คำนวณค่าของ discriminant
  12. discriminant = b * b - 4 * a * c;
  13.  
  14. // ตรวจสอบค่า discriminant
  15. if (discriminant > 0) {
  16. // มีสองรากจริงที่แตกต่างกัน
  17. root1 = (-b + sqrt(discriminant)) / (2 * a);
  18. root2 = (-b - sqrt(discriminant)) / (2 * a);
  19. printf("Roots are real and different.\n");
  20. printf("Root1 = %.2f and Root2 = %.2f\n", root1, root2);
  21. } else if (discriminant == 0) {
  22. // มีรากสองรากเท่ากัน
  23. root1 = root2 = -b / (2 * a);
  24. printf("Roots are real and same.\n");
  25. printf("Root1 = Root2 = %.2f\n", root1);
  26. } else {
  27. // ไม่มีรากจริง
  28. float realPart = -b / (2 * a);
  29. float imaginaryPart = sqrt(-discriminant) / (2 * a);
  30. printf("Roots are complex and different.\n");
  31. printf("Root1 = %.2f + %.2fi and Root2 = %.2f - %.2fi\n", realPart, imaginaryPart, realPart, imaginaryPart);
  32. }
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
Enter coefficients a, b, and c: Roots are real and different.
Root1 = inf and Root2 = -2620724014927307183482545578704896.00