Here you will get C program to find roots of quadratic equation ax^{2}+bx+c=0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include<stdio.h> #include<math.h> int main() { float root1,root2,a,b,c,d,imaginaryPart,realPart; printf("Quadratic Equation is ax^2+bx+c=0"); printf("\nEnter values of a,b and c:"); scanf("%f%f%f",&a,&b,&c); d=(b*b)-(4*a*c); if(d>0) { printf("\nTwo real and distinct roots"); root1=(-b+sqrt(d))/(2*a); root2=(-b-sqrt(d))/(2*a); printf("\nRoots are %f and %f",root1,root2); } else if(d==0) { printf("\nTwo real and equal roots"); root1=root2=-b/(2*a); printf("\nRoots are %f and %f",root1,root2); } else{ printf("\nRoots are complex and imaginary"); realPart = -b/(2*a); imaginaryPart = sqrt(-d)/(2*a); printf("\nRoots are %.2f+%.2fi and %.2f-%.2fi", realPart, imaginaryPart, realPart, imaginaryPart); } return 0; } |

**Output**

*Quadratic Equation is ax^2+bx+c=0*

*Enter values of a,b and c:1*

*1*

*1*

*Roots are complex and imaginary*

*Roots are -0.50+0.87i and -0.50-0.87i*

Hi, I’ve a doubt in this program. When you find the value for imaginary root, why to use minus in square root (sqrt(-d))? Because the value which you get from d will already be in negative. Please make my doubt clear. I know it won’t work without negative sign but why it is like that?