C ++ classroom practice three

[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;
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2177 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.