Quadratic Equation Roots Program in C

This Quadratic Equation Roots Program in C explains A quadratic equation with real or complex coefficients has two solutions, called roots. These two solutions may or may not be distinct, and they may or may not be real.


#include <stdio.h>
#include <math.h>
void main()
{
double a, b, c, determinant, root1,root2, real, imaginary;
printf(“Enter co-efficients a, b and c : “);
scanf(“%lf %lf %lf”,&a, &b, &c);
determinant = b*b-4*a*c;
/* condition for real and different roots */
if (determinant > 0)
{
/* sqrt() function returns square root */
root1 = (-b+sqrt(determinant))/(2*a);
root2 = (-b-sqrt(determinant))/(2*a);
printf(“root1 = %.2lf and root2 = %.2lf”,root1 , root2);
}
/* condition for real and equal roots */
else if (determinant == 0)
{
root1 = root2 = -b/(2*a);
printf(“root1 = root2 = %.2lf;”, root1);
}
/* if roots are not real */
else
{
realPart = -b/(2*a);
imaginary = sqrt(-determinant)/(2*a);
printf(“root1 = %.2lf+%.2lfi \n”, real, imaginary);
printf(“root2 = %.2f-%.2fi \n”, real, imaginary);
}
}

Share this