fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double solve1(double a,double b,double c){
  5. if(a==0){
  6. return -c/b;
  7. }
  8. if(b*b-4*a*c<0){
  9. printf("Complex\n");
  10. return 0;
  11. }
  12. return (-b+sqrt(b*b-4*a*c))/2/a;
  13. }
  14.  
  15. double solve2(double a,double b,double c){
  16. if(a==0){
  17. return -c/b;
  18. }
  19. if(b*b-4*a*c<0){
  20. printf("Complex\n");
  21. return 0;
  22. }
  23. return (-b-sqrt(b*b-4*a*c))/2/a;
  24. }
  25.  
  26. int main(void){
  27. double a,b,c,x1,x2;
  28. a=1.0;
  29. b=3.0;
  30. c=2.0;
  31. x1=solve1(a,b,c);
  32. x2=solve2(a,b,c);
  33. printf("x1=%lf\n",x1);
  34. if(x1!=x2){
  35. printf("x2=%lf\n",x2);
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
x1=-1.000000
x2=-2.000000