Find the roots of a quadratic equation using Switch statement. (D=0,D>0,D < 0)
#include < stdio.h>
#include
/* Used for sqrt() */
int main()
{
float a, b, c;
float root1, root2, imaginary,real,imag;
float discriminant;
printf("Enter values of a, b, c of quadratic equation ");
scanf("%f%f%f", &a, &b, &c);
/* Calculate discriminant */
discriminant = (b * b) - (4 * a * c);
/* Compute roots of quadratic equation based on the nature of
discriminant */
switch(discriminant > 0)
{
case 1:
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two distinct and real roots exists: %.2f and %.2f",root1,
root2);
break;
case 0:
switch(discriminant < 0)
{
case 1:
printf("Two distinct complex roots exists \n");
real = -b/(2*a);
imag = sqrt(fabs(discriminant))/(2*a);
printf("Root1 = %.2f + i%.2f”,real, imag \n");
printf("Root1 = %.2f - i%.2f”,real, imag \n");
break;
case 0:
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exists: %.2f and %.2f",
root1, root2);
break;
}
}
return 0;
}