#include <iostream>
#include <cmath>
using namespace std;
int main () {
// we declare an array for store coefficients of quadratic equation
// for example x^2+5x+6=0
int coefficients[3] = {1, 5, 6};
float root1, root2;
float discriminant;
discriminant = pow(coefficients[1], 2) - 4 * coefficients[0]*coefficients[2];
if (discriminant > 0) {
cout << "The quadratic equation has two real numbers and they are: " << endl;
root1 = (-coefficients[1] + sqrt(discriminant)) / 2 * coefficients[0];
root2 = (-coefficients[1] - sqrt(discriminant)) / 2 * coefficients[0];
cout << root1 << " " << root2;
}
else if (discriminant == 0) {
cout << "The quadratic equation has two identical roots and it is: " << endl;
cout << -coefficients[1] / 2. * coefficients[0];
}
else {
cout << "The quadratic equation hasn't roots";
}
return 0;
Comments
Leave a comment