Hi everybody. I made this code for quadratic formula but it's not working as it should. I am learning "C language" so it's very simple code.
Important: It is working efficiently for "real and equal" and "real and equal" roots. But for "complex roots" it's not true for every value of a, b, c.
Example: for a=1,b=2,c=3; it prints "The first root = -1 + 1.414214 i"
and "The second root = -1 - 1.414214 i", which is accurate result.
But for a=2,b=3,c=4; it prints correct answer for imaginary part but "0.0000" for both the real parts and wrong results for other values. Please help
Note: Please don't post brand new code in reply, because I don't have full command on C language. Just make change where needed in my code and reply this post.
Regards: arslan20
Code:#include<stdio.h> #include<conio.h> #include<math.h> main() { int a,b,c,d; float x1,x2; printf("Enter value of a : "); scanf("%d",&a); printf("Enter value of b : "); scanf("%d",&b); printf("Enter value of c : "); scanf("%d",&c); d = pow(b,2) - 4 * a *c; if(d==0) { printf("\nThe roots are real and equal"); x1 = -b/(2*a); printf("\nThe first root = %f",x1); printf("\nThe second root = %f",x1); } else if(d>0) { printf("\nThe roots are real and unequal"); x1 = (-b+sqrt(d))/(2*a); x2 = (-b-sqrt(d))/(2*a); printf("\nThe first root = %f",x1); printf("\nThe second root = %f",x2); } else { printf("\nThe roots are complex"); d = -d; x1 = -b/(2*a); x2 = sqrt(d)/(2*a); printf("\nThe first root = %f + %f i",x1,x2); printf("\nThe second root = %f - %f i",x1,x2); } getch(); }



3Likes
LinkBack URL
About LinkBacks


