# C code for quadratic formula:

This is a discussion on C code for quadratic formula: within the C Programming forums, part of the General Programming Boards category; Hi everybody. I made this code for quadratic formula but it's not working as it should. I am learning "C ...

1. ## C code for quadratic formula:

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();
}

2. Change a line to this
x1 = (float)-b/(2*a);

The problem you have is your existing expression is all done using integer arithmetic, so any fraction is lost before it gets assigned to x1

3. ## Great thanks

Thank you Salem: It worked great for me. 110% helpful and true answer. But you know I am beginner in C language so i don't understand the reason. Although you mentioned it but my level is low. It will be great if you come up to my level and give me reason that why (float) is necessary for complex roots but not for real one. Thanks once again....

4. It is to do with how you did the calculations for the real roots.

In the statements
Code:
x1 = (-b+sqrt(d))/(2*a);
x2 = (-b-sqrt(d))/(2*a);
sqrt() is a function that accepts an argument of type double, and returns a double. The compiler therefore converts d to type double, passes the result to sqrt(), which returns double. Since sqrt(d) is of type double, the value of -b is also converted to type double before adding it to sqrt(d). 2*a is computed as an int. Then since (-b + sqrt(d)) is of type double, the value of 2*a is converted to type double before doing the division. The result is that (-b+sqrt(d))/(2*a) is computed with a value of type double. x1 is of type float, so that result is converted to float and the result is stored in x1.

A similar sequence of events occurs in the calculation of x2.

In contrast, your calculation for complex roots
Code:
d = -d;
x1 = -b/(2*a);
x2 = sqrt(d)/(2*a);
d = -d; is performed purely using integer arithmetic. The calculation of x1, since b and a are both of type int, computes (2*a) as an int, computes -b as an int, and then divides the two using integer arithmetic. No conversion to double occurs in that calculation. The calculation of x2, however, converts d to a value of type double, computes sqrt(d) as a value with type double, converts the integer (2*a) to double before doing the division, converts the result of the division to float in order to store it in x2.

Salem's change
Code:
x1 = (float)-b/(2*a);
forces -b to be converted to float. The compiler therefore converts the int value (2*a) to float before performing the division, which means the result of division has type float - which is then stored without conversion in x1.

All of those conversions before performing operations result from basic rules of the C language.

The basic concept is that all calculations involving values of type int will be done integer integer arithmetic. However, if an expression with an operation (addition, multiplication, etc) involves a value of type int and a value of type double, the int will be converted to double before performing the operation.

5. ## Right way

Thanks grumpy: you explained it right way. You should be a teacher because of your method of explaining. well done!!