/* 5(a) W.A.P to find
all roots of a Quadratic Equation Ax2 + Bx + C = 0, for any non-zero
co-efficients A, B & C else report error */
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,d,x1,x2,rp,ip;
int choice;
clrscr();
printf("Input values of a,b & c : ");
scanf("%f %f %f",&a,&b,&c);
if ((a==0) && (b==0) && (c==0))
{
printf("!!! Invalid Entry. Please try again !!!");
exit();
}
else if (a>0)
{
d=b*b-4*a*c;
if (d>0) choice=1;
if (d==0) choice=2;
if (d<0) choice=3;
switch(choice)
{
case 1:
printf("\n\n*** Roots are real and distinct ***");
x1 = ( -b + sqrt(d) ) / (2 * a);
x2 = ( -b - sqrt(d) ) / (2 * a);
printf("\nRoots are x1 = %f and x2 = %f",x1,x2);
break;
case 2:
printf("\n\n*** Roots are real and equal ***");
x1 = ( -b ) / (2 * a);
x2 = x1;
printf("\nRoots are x1 = %f and x2 = %f",x1,x2);
break;
case 3:
printf("\n\n*** Roots are imaginary ***");
rp = ( -b ) / (2 * a);
ip = sqrt(fabs(d)) / (2 * a);
printf("\nRoots are x1 = %f + i%f and x2 =%f - i%f",rp,ip,rp,ip);
break;
}
}
else
printf("\n\nIt's not a Quadratic Eqn.\nRoot of the equation is %f",-(c/b));
getch();
}