Write a complete C+ program which will allow the user to enter on tho koyboard t
ID: 3919436 • Letter: W
Question
Write a complete C+ program which will allow the user to enter on tho koyboard the coofficicnts a, b, and e of a quadratic function. The program should then compute and display on the ceen the roots 1 and T2 of the quadratic. A sample run of your program should look like the following: Enter quadratic coefficients a,b,c: -0.5 1 1.5 Roots are x1-3, x21 or the following: ???er quadratic coefficients a,b,c: 2 4 10 Roots are xi1 2j, x21 2j (numbers after the colon were ontered at the keyboard in eacli casc) Namc your source code filc quad.cpp and place comment. top with vour namoExplanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a, b, c, x1, x2, dis, real, imaginary;
cout << "Enter quadratic coefficients a,b,c: ";
cin>>a;
cin>>b;
cin>>c;
dis = b*b - 4*a*c;//finding discriminant
if (dis > 0) {//means roots are real..
x1 = (-b + sqrt(dis)) / (2*a);
x2 = (-b - sqrt(dis)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (dis == 0) {//means roots are same
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(dis)) / (2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {//no real roots are possible...
real = -b/(2*a);
imaginary =sqrt(-dis)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << real << "+" << imaginary << "i" << endl;
cout << "x2 = " << real << "-" << imaginary << "i" << endl;
}
return 0;
}
output:
Enter quadratic coefficients a,b,c: -0.5 1 1.5
Roots are real and different.
x1 = -1
x2 = 3
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.