Suppose assume a equation ax^2+bx+c=0
if b^2-4ac=0 then the roots are real and equal
if b^2-4ac<0 then the roots are imaginary and unequal
if b^2-4ac>0 then the roots are real and unequal
#include <stdio.h>
#include <math.h>
int main(){
float a,b,c,d,root1,root2,realpart,imagpart;
printf("Enter coefficients a,b and c: ");
scanf("%f %f %f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0){
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("root1=%.2f and root2=%.2f",root1,root2);
}
else if(d==0){
root1=root2=-b/(2*a);
printf("root1=root2=%.2f",root1);
}
else{
realpart=-b/(2*a);
imagpart=sqrt(-d)/(2*a);
printf("root1=%.2f+%.2fi and root2=%.2f-%.2fi",realpart,imagpart,realpart,imagpart);
}
}
Output:
Enter coefficients a,b and c: 3
9
12
root1=-1.50+1.32i and root2=-1.50-1.32i