Revising Quadratic Equation

All Test cases are not passed. could you please tell me why?

int main()
{
signed long int a,b,c,d,x1,x2;
cin>>a>>b>>c;

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

x1 = (-b+d)/(2*a);
x2 = (-b-d)/(2*a);

if(d>0)
{
	cout<<"Real and Distinct"<<endl;
	if(x1<x2)
	{
		cout<<x1<<" "<<x2;
	}
	else
	{
		cout<<x2<<" "<<x1;
	}
}

else if(d=0)
{
	cout<<"Real and Equal"<<endl;
	if(x1<x2)
	{
		cout<<x1<<" "<<x2;
	}
	else
	{
		cout<<x2<<" "<<x1;
	}
}

else
{
	cout<<"Imaginary"<<endl;
}
return 0;

}

1 Like

Hi @sharma.harsh3107

Take d as (bb-4ac) NOT sqrt(bb-4ac).

but formula is (-b-(bb-4ac)^(1/2)) / 2a

Yes, but when you take d as (bb-4ac), put value of x1 and x2 as (-b+sqrt(d))/2a and (-b-sqrt(d))/2a respectively.

And for checking nature of roots:

we do NOT check the nature of roots by sqrt(bb-4ac),
but by d=(bb-4ac)

check for d==0,d>0 and d<0.

And check for d==0 instead of d=0.

Here’s the link to the complete solution if you want to check it out.

https://ide.codingblocks.com/s/164546
Like if this solved your problem.

1 Like