In: Computer Science
quadratic equation: ax2 + bx + c =0
where x = ( -b± sqrt(b2-4*a*c) ) / 2*a
number of results obtained when solving the quadratic equation: ax2 + bx + c =0 with given real inputs a, b, and c.
c++
code:
#include <iostream>
#include <cmath>
using namespace std;
void roots(int a, int b, int c){
// prints root of quadratinc equation ax*2+bx+c
if (a == 0){
cout << "Invalid";
return;
}
int d = b*b - 4*a*c;
double sqrt_value = sqrt(abs(d));
if (d > 0){
cout<< "Roots are real and different \n";
cout << (double)(-b + sqrt_value)/(2*a)<<"\n"<<
(double)(-b - sqrt_value)/(2*a);
}
else if (d == 0){
cout<<"Roots are real and same\n";
cout<<-(double)b/(2*a);
}
else // d<0
{
cout<<"Roots are complex \n";
cout<<-(double)b/(2*a) <<" +
i"<<sqrt_value<<"\n"<<-(double)b/(2*a) <<"-
i" <<sqrt_value;
};
}
int main()
{
int a , b , c;
cout<<"enter the co-efficient a,b and c values:\n";
cin >> a >> b >> c;
roots(a,b,c);
return 0;
}
case 1:
output:
enter the co-efficient a,b and c values:
1
-7
12
Roots are real and different
4
3
case 2:
output:
enter the co-efficient a,b and c values:
2
4
2.3
Roots are real and same
-1
case 3:
output:
enter the co-efficient a,b and c values:
1
4
5.6
Roots are complex
-2 + i2
-2- i2
case 4:
output:
enter the co-efficient a,b and c values:
0
1
3
Invalid