[Content in this section] Use of if…else…
[Exercise] Solving quadratic equations in one variable
[Code]
#include
#include
using namespace std;
int main() {
double a, b, c;
double delta;
double x1, x2, x;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
cout <<"does not form an equation"<< endl;
}
else {
x = -c/b;
cout << "The roots of a linear equation in one variable are: x=< /span>" << x << endl;
}
}
else {
delta = b * b-4.0 * a * c;
if (delta >= 0) {
delta = sqrt(delta);
x1 = (-b + delta) / 2.0 / a;
x2 = (-b-delta) / 2.0 / a;
cout << "The equations have real roots, which are: span>" << " x1=" << x1 << " x2=" << x2 << endl;
}
else {
delta = sqrt(-delta);
x1 = -b / 2.0 / a;
x2 = delta / 2.0 / a;
cout << "Equations have complex roots, which are: span>" << " x1=" << x1 << "+j" << x2 << " span> x2=" << x1 << "-j" << x2 <<< span style="color: #000000;"> endl;
}
}
return 0;
}
#include
#include
using namespace std;
int main() {
double a, b, c;
double delta;
double x1, x2, x;
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
cout <<"does not form an equation"<< endl;
}
else {
x = -c/b;
cout << "The roots of a linear equation in one variable are: x=< /span>" << x << endl;
}
}
else {
delta = b * b-4.0 * a * c;
if (delta >= 0) {
delta = sqrt(delta);
x1 = (-b + delta) / 2.0 / a;
x2 = (-b-delta) / 2.0 / a;
cout << "The equations have real roots, which are: span>" << " x1=" << x1 << " x2=" << x2 << endl;
}
else {
delta = sqrt(-delta);
x1 = -b / 2.0 / a;
x2 = delta / 2.0 / a;
cout << "Equations have complex roots, which are: span>" << " x1=" << x1 << "+j" << x2 << " span> x2=" << x1 << "-j" << x2 <<< span style="color: #000000;"> endl;
}
}
return 0;
}