Advertisement
bangkieu96

Untitled

Sep 8th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "math.h"
  3.  
  4. int main()
  5. {
  6.     float a, b, c, d, x1, x2;
  7.     printf("Enter a, b, c for the quadratic equation ax^2 + bx + c = 0\n");
  8.     printf("a = "); scanf ("%f", &a);
  9.     printf("b = "); scanf ("%f", &b);
  10.     printf("c = "); scanf ("%f", &c);
  11.     if (a==0)
  12.     {
  13.         x1=-c/b;
  14.         printf("\nThe equation has one root.\nx = %.3f", x1);
  15.     }
  16.     else
  17.     {
  18.         d=b*b-4*a*c;
  19.         if (d<0)
  20.         {
  21.             x1=-b/(2*a);
  22.             x2=(sqrt(-d))/(2*a);
  23.             printf("\nThe equation has two distinct complex roots.\n");
  24.             printf("x1 = %.3f + %.3fi\n", x1, x2);
  25.             printf("x2 = %.3f - %.3fi", x1, x2);
  26.         }
  27.         else if (d==0)
  28.         {
  29.             x1=-b/(2*a);
  30.             printf("\nThe equation has exactly one real root.\nx = %.3f", x1);
  31.         }
  32.         else
  33.         {
  34.             x1=(-b+sqrt(d))/(2*a);
  35.             x2=(-b-sqrt(d))/(2*a);
  36.             printf("\nThe equation has two distinct real roots.\n");
  37.             printf("x1 = %.3f\n", x1);
  38.             printf("x2 = %.3f", x2);
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement