# Thread: Can't Write this program

1. ## Can't Write this program

I am supposed to write a program in C using an algorithm. It is supposed to solve the quadratic equation when getting a,b,c. However, when I put in a =1, b=4, c=4, the program doesn't work. In other words, the last two else's of the program don't work.
Code:
```
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include <math.h>

main()
{
double a,b,c,d,x,x1,x2;
printf("Enter the value for a: ");
a=GetReal();
printf("Enter the value for b: ");
b=GetReal();
printf("Enter the value for c: ");
c=GetReal();
if (a==0)
{
if (b==0)
{
printf("No solution");
}
else
{
x=(-c/b);
printf("The equation is not quadratic and the solution is:%.2f",x);
}
}
else
{
d=((b*b)-(4*a*c));
if (d<0)
{
printf("There is no real solution.");
}
else
{
if (d>0)
{
x1=(-b+(sqrt(d)))/(2*a);
x2=(-b-(sqrt(d)))/(2*a);
printf("There are 2 solutions. The first is %.2f and the second is %.2f",x1,x2);
}
else
{
if (d=0)
{
x1=(-b/(2*a));
printf("There is one solution which is %.2f");
}
}
}

}

getchar();
}```

2. > if (d=0)
Try it with
d == 0

Or better yet, just remove the condition altogether, since it's the only condition left after the <0 and >0 tests.

> printf("There is one solution which is %.2f");
Where is the parameter to print with %f ?

3. Originally Posted by Salem
> if (d=0)
Try it with
d == 0

Or better yet, just remove the condition altogether, since it's the only condition left after the <0 and >0 tests.

> printf("There is one solution which is %.2f");
Where is the parameter to print with %f ?
I changed it to == 0 and when i put a=1, b=4, c=4, I get the answer as 0 when it should be -2. I'm not sure what is wrong.

4. Well there were two mistakes, and you said you only fixed one of them.

Plus, simply describing your change rather than posting the actual code is not really informative.

you missed his use of

Code:
`main()`
and lack of a return

6. Give the user a chance. Print out "ax^2 + bx + c = 0" then ask for a, b and c.

7. Originally Posted by Salem
Well there were two mistakes, and you said you only fixed one of them.

Plus, simply describing your change rather than posting the actual code is not really informative.
I have now fixed both of those things and it is working. Thank you very much!!!

Code:
```#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include <math.h>

main()
{
double a,b,c,d,x,x1,x2;
printf("Enter the value for a: ");
a=GetReal();
printf("Enter the value for b: ");
b=GetReal();
printf("Enter the value for c: ");
c=GetReal();
if (a==0)
{
if (b==0)
{
printf("No solution");
}
else
{
x=(-c/b);
printf("The equation is not quadratic and the solution is:%.2f",x);
}
}
else
{
d=((b*b)-(4*a*c));
if (d<0)
{
printf("There is no real solution.");
}
else
{
if (d>0)
{
x1=(-b+(sqrt(d)))/(2*a);
x2=(-b-(sqrt(d)))/(2*a);
printf("There are 2 solutions. The first is %.2f and the second is %.2f",x1,x2);
}
else
{
if (d==0)
{
x1=(-b/(2*a));
printf("There is one solution which is %.2f",x1);
}
}
}

}

getchar();
}```