The roots of a quadratic equation are real if discriminator b^{2}-4ac is greater than or equal to zero; otherwise they are complex conjugate. However, if the roots are complex conjugate, i.e., if b2-4ac<0, the evaluation of expression sqrt (b*b-4*a*c) causes a domain error and the program halts abruptly.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
double a,b,c,d,root1,root2;
clrscr();
printf("Enter Value for a : ");
scanf("%lf", &a);
printf("Enter Value for b : ");
scanf("%lf", &b);
printf("Enter Value for c : ");
scanf("%lf", &c);
if (a==0.0)
{
printf ("\n a Should NOT be 0");
exit(0);
}
d = b * b - 4 * a * c;
if ( d > 0.0 )
{
d = sqrt( d );
root1 = ( -b + d ) / ( 2 * a );
root2 = ( -b - d ) / ( 2 * a );
printf("\nThe Roots %.4lf and %.4lf were Found\n\n", root1, root2 );
}
else if ( d == 0.0 )
{
root1 = -b / ( 2 * a );
printf("\nA Single Root, %.4lf, was Found\n\n", root1 );
}
else
{
printf("\nNo real roots were found\n\n");
}
getch();
}