#include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c,d,root1,root2; clrscr(); printf("Enter a b c \n"); scanf("%f%f%f",&a,&b,&c); d=b*b-4*a*c; /* finding determinant */ if(d==0) /* when d is 0 both the roots will be equal */ { printf("\n roots are equal \n"); root1=root2=-b/(2*a); printf("root1=%.3f \n root2=%.3f", root1,root2); } else if(d>0) /* roots are real and distinct */ { printf("\n roots are real and distinct \n"); root1=(-b+sqrt(d))/(2*a); root2=(-b-sqrt(d))/(2*a); printf("root1=%.3f\n root2=%.3f",root1,root2); } /* when d is -ve it is not possible to take the squareroot of -ve number, we have to take absolute value of d */ else { printf("\n roots are imaginary \n"); root1=-b/(2*a); root2=sqrt(-d)/(2*a); printf("real part=%.3f \n imaginary part = %.3f", root1,root2); } getch(); } Output 1: Enter a b c 2 2 2 roots are imaginary real part=-0.500 imaginary part=0.866 Output 2: Enter a b c 2 5 1 roots are real and distinct root1=-0.219 root2=-2.281 Output 3: Enter a b c 1 2 1 roots are equal root1=-1.000 root2=-1.000