Computer Science‎ > ‎

## C Program to find the roots of Quadratic Equations

```/*The following program finds out the roots of a quadriatic equation by taking the coefficents as the input
from the user.*/
#include<stdio.h>
#include<math.h>
int main()
{
double a,b,c;
printf("Enter the coefficents of the quadriatic equation of the form ax^2+bx+c\n");
scanf("%lf%lf%lf",&a,&b,&c);//We ask for the coefficents from the user
double D=pow(b,2)-4*a*c;
if(D<0)//We check the determinant whether it is greater or lesser than zero and accordingly find real or imaginary roots
{
printf("The roots are imaginary\n");
double real,img;
real=(-1*b)/(2*a);//The real part of the root
img=sqrt(-1*D)/(2*a);//The imaginary part of the root
printf("The first root is %lf+i%lf\n",real,img);
printf("The first root is %lf-i%lf\n",real,img);
}
if(D>=0)
{
printf("The roots are real\n");
if(D!=0)
{   //We find both roots if D>0
double root1=(-1*b+sqrt(D))/(2*a);
double root2=(-1*b-sqrt(D))/(2*a);
printf("The first root is %lf\n",root1);
printf("The second root is %lf\n",root2);
}
else
{   //We find the root if D==0 and display that we have equal roots
double root=(-1*b)/(2*a);
printf("The roots are equal\n");
printf("The root is %lf\n",root);
}
}
getch();
}
/*A sample run of the program is found to work as follows:-
Enter the coefficents of the quadriatic equation of the form ax^2+bx+c
1 5 6
The roots are real
The first root is -2.000000
The second root is -3.000000
*/```