Given coefficients of a quadratic equation , you need to print the nature of the roots (real and distinct , real and equal , imaginary)

my code passed 2 testcases but isnt passing all
can someone tell the mistakein code :
#include
#include<math.h>
using namespace std;
int main() {
float a,b,c,x,d,e;
cin>>a>>b>>c;
if(-100<= a,b,c <=100){
int x =(bb - 4ac);
d= (-b+sqrt(x))/(2
a);
e= (-b-sqrt(x))/(2a);
if(b
b > 4ac){
cout<<“Real and Distinct\n”;
cout<<d<<" “<<e;
}
else if(bb == 4a*c){
cout<<“Real and Equal\n”;
cout<<d<<” "<<e;
}
else{
cout<<“Imaginary\n”;
}
}
return 0;
}

same problem
my code:-

#include<bits/stdc++.h>
using namespace std;

// Prints roots of quadratic equation ax*2 + bx + x
void findRoots(int a, int b, int c)
{
// If a is 0, then equation is not quadratic, but
// linear
if (a == 0)
{
cout << “Invalid”;
return;
}

int d = b*b - 4*a*c;
double sqrt_val = sqrt(abs(d));

if (d > 0)
{
	cout << "Real and distinct \n";
	cout << (double)(-b - sqrt_val)/(2*a) << " "
		<< (double)(-b + sqrt_val)/(2*a);
}
else if (d == 0)
{
	cout << "Real and same \n";
	cout << -(double)b / (2*a);
}
else // d < 0
{
	cout << "Complex \n";
	cout << -(double)b / (2*a) << " - i" << sqrt_val
		<< "\n" << -(double)b / (2*a) << " + i"
		<< sqrt_val;
}

}

// Driver code
int main()
{
int a , b , c;
cin>>a>>b>>c;

findRoots(a, b, c);
return 0;

}

1 Like

cout<<min(d,e)<<" "<<max(d,e);
use this to print because its asked to print in ascending order

1 Like

first of all your condition for real and distinct roots is wrong try comparing it with 0
second in case of real and distinct save both roots in array of size 2 and sort the array and then print the array

1 Like

your condition for real and distinct roots is wrong try comparing it with 0
in case of real and distinct save both roots in array of size 2 and sort the array and then print the array

1 Like