Quadratic Equation
Quadratic Equation
Algorithm:
Step 1: Start
Step 6: Check if disc is greater than zero or not. If true, then go to Step 7. Otherwise, go to
Step 8.
root2 = (-b-sqrt(disc))/(2*a)
Step 8: Compute the complex and distinct roots. Compute the real part, r_part = (-b)/(2*a)
Step 9: Stop
Program
#include<stdio.h>
#include<math.h>
void main()
{
float a, b, c, root1, root2, r_part, i_part, disc;
printf("Enter the 3 non-zero coefficients\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0 || b==0 || c==0)
{
printf(“Co-efficients cannot be 0\n”);
return;
}
disc=(b*b)-(4*a*c);
if(disc==0)
{
printf("Roots are equal\n");
root1=-b/(2*a);
root2=root1;
printf("root1=root2=%f",root1);
}
else if(disc>0)
{
printf("Roots are real and distinct\n");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("root1 = %f \t root2 = %f\n",root1,root2);
}
else
{
printf("Roots are complex\n");
r_part=-b/(2*a);
i_part=(sqrt(-disc))/(2*a);
printf("root1 = %f+i%f \n root2 = %f-i%f\n",r_part,i_part,r_part,i_part);
}
}