Write the C program that solve the quadratic equation problem

The output should appear as follows:

Enter A: 3

Enter B: 5

Enter C: 2

There are two solutions: -0.666667 and -1.

Press any key to continue_

thank you,

qqwushi12345
0
Newbie Poster

Write the C program that solve the quadratic equation problem

The output should appear as follows:

Enter A: 3

Enter B: 5

Enter C: 2

There are two solutions: -0.666667 and -1.

Press any key to continue_

thank you,

Jump to PostOf course we're not gonna do this, unless you pay me $20.00 dollars for this problem.

But to start you off, this is the quadratic equation :

`x = -b/(2a) +- sqrt(b^2 - 4ac) / (2a).`

Since they gave you a,b, and c. You just have …

Jump to PostYour problem is assuming that x1 and x2 are integers when you print them out.

…

Additionally, a,b,c are all likely to be floating point numbers. So read them in as floats. So make your output statement`printf("%g and %g\n",x1,x2);`

Also you can use the sqrt function in line`k=sqrt(d);`

.

mrnutty
761
Senior Poster

Of course we're not gonna do this, unless you pay me $20.00 dollars for this problem.

But to start you off, this is the quadratic equation :

`x = -b/(2a) +- sqrt(b^2 - 4ac) / (2a).`

Since they gave you a,b, and c. You just have to plug it in though.

A word of caution this part of the equation *b^2 - 4ac * is called the discriminant. Make sure this is positive else the solution is imaginary.

qqwushi12345
0
Newbie Poster

Below is my code for the problem, please correct my mistake:

```
#include <stdafx.h>
#include "simpio.h"
#include "genlib.h"
#include "math.h"
main()
{
float a,b,c,d,k,x1,x2;
float pow(float,float);
printf("Enter A: ");
a=GetInteger();
printf("Enter B: ");
b=GetInteger();
printf("Enter C: ");
c=GetInteger();
d=(b*b-4*a*c);
k=pow(d,1./2);
x1=(-b+k)/(2*a);
x2=(-b-k)/(2*a);
printf("%d and %d",x1,x2);
}
```

please correct my mistake,thank you.

Edited
by peter_budo because:
*
Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) *

StuXYZ
731
Practically a Master Poster

Your problem is assuming that x1 and x2 are integers when you print them out.

Additionally, a,b,c are all likely to be floating point numbers. So read them in as floats. So make your output statement `printf("%g and %g\n",x1,x2);`

Also you can use the sqrt function in line `k=sqrt(d);`

.

HOWEVER: Can I just point out that using the formula given is a horrible way to calculate the roots to a quadratic equation due to the numeric instability of the solution. The "better" way is to calculate x1,x2 and determine which biggest root

(either in terms of magnitude or accuracy as a solution) and use the relationship `x1*x2=c/a`

to get the other root.

There are further tricks to getting better roots from a quadratic equation, particularly if the roots are close, if b^2-4ac is near zero ,e.g a Newton Raphson step etc.

Finally, please use code tags.

moseshenry
-4
Newbie Poster

#include<iostream.h>

#include<math.h>

main()

{

float a,b,c,d,k,x,y;

printf("Enter the values of a,b,c:");

scanf("%f%f%f",&a,&b,&c);

d = ((b*b)-(4*a*c));

if(d>=0)

{

k = sqrt(d);

x = (-b+k)/(2*a);

y = (-b-k)/(2*a);

printf("The quadratic equation is x = %f and y = %f",x,y);

}

else

{

float re, im;

d*=-1;

k = sqrt(d);

re = -b/(2*a);

im = k/(2*a);

printf("The quadratic equation is x=%f+(%fi)\n",re,im);

printf("The quadratic equation equation is y=%f-(%fi)\n",re,im);

}

return 0;

}

WaltP
commented:
No code tags; resurrecting old thread; posting an alledged answer in full to help him cheat. Good Job :-(
-4

Taywin
312
Posting Virtuoso

That's not what this forum for... You should read the forum rules before you post the code like that...

Edited
by Taywin because:
*
n/a *

1moinul
0
Newbie Poster

mohanned_2
0
Newbie Poster

hello all

can help me to writing c code for this eq. please

Edited
by mohanned_2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.