What is the problem in this program of Quadratic equation ?

It is only running the first condition(disc<0)

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2,disc;
cout<<"Enter the value of a = ";
cin>>a;cout<<"\n";
cout<<"Enter the value of b = ";
cin>>b;cout<<"\n";
cout<<"Enter the value of c = ";
cin>>c;
disc=sqrt(b*b-4*a*c);
if(disc<0)
{
cout<<"\nRoots Are Complex";
}
if (disc=0)
{
cout<<"\nRoots Are Equal";
x1=-b/(2*a);
x2=-b/(2*a);
cout<<"\n"<<x1<<"\n";
cout<<"\n"<<x2<<"\n";
}
if(disc>0)
{
cout<<"\nRoots are real and unequal";
x1=-b+disc/(2*a);
x2=-b-disc/(2*a);
cout<<"\n"<<x1<<"\n";
cout<<"\n"<<x2<<"\n";
}

getch();
}
In the second if you have disc=0 which assigns 0 to disc and is evaluated as value 0 so the condition is false. Then the next condition is false. Use disc==0
the other problem is you are making disc=sqrt(...) if the argument to sqrt is negative than this return nan (i.e. not a number). You need to test the argument to the sqrt.

Also these problems are easy to solve with a debugger... try to get your hands on one...
disc >= 0 by definition of sqrt().

These two expressions are missing parentheses:
1
2
x1=-b+disc/(2*a);
x2=-b-disc/(2*a);
Topic archived. No new replies allowed.