### C Code Snippets 2 - Finding Roots of Quadratic Equations

Hello everyone, in this post I am going to share a flow chart and a source code on how to find the roots of a quadratic equation. I think the flow chart and the code are self-explanatory. If you have any questions do not hesitate to write a comment below.

``````
/**
* Author : Berk Soysal
*/

#include <stdio.h>
#include <math.h> /* This is needed to use sqrt() function.*/
void main()
{
float a, b, c, delta, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);

if(a ==0 || b==0)
{
printf("\n\nPlease enter nonzero values !\n");
}

else
{
delta=b*b-4*a*c;
if (delta>0)
{
r1= (-b+sqrt(delta))/(2*a);
r2= (-b-sqrt(delta))/(2*a);
printf("\n\nRoots are: %.2f and %.2f\n\n",r1 , r2);
}
else if (delta==0)
{
r1 = r2 = -b/(2*a);
printf("\n\nRoots are: %.2f and %.2f\n\n", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-delta)/(2*a);
printf("\n\nRoots are: %.2f + %.2fi and %.2f - %.2fi\n\n", real, imag, real, imag);
}

}
}

``````
Keywords: CCode Snippets
###### Author: Berk Soysal

Software Developer, Codemio Admin