Friday, May 21, 2010

C language! help?

source code for a program that computes for the roots of a quadratic equation, without using factoring method and quadratic formula

C language! help?
Try something like this:





#include %26lt;stdio.h%26gt;


#include %26lt;math.h%26gt;





int main()


{


// declare variables





double radicand, x1, x2, a, b, c;





// get coefficents





printf("Enter a non-zero value for a:\n");


scanf("%lf", %26amp;a);





printf("Enter the value for b:\n");


scanf("%lf", %26amp;b);





printf("Enter the value for c:\n");


scanf("%lf", %26amp;c);





// compute radicand and roots





radicand = b * b - 4 * a * c





if (radicand %26lt; 0)


printf("Your equation produeced complex zeroes.\n")


else


{


x1 = (-b + sqrt(radicand)) / (2 * a);


x2 = (-b - sqrt(radicand)) / (2 * a);





printf "x1 = %lf\n", x1);


printf "x2 = %lf\n", x2);


}








You may need to add error checks or contingencies for complex rotos as well, but this should give you the general idea. When compiling you may need at add the -lm switch.


No comments:

Post a Comment