Write a complete C++ program that solves a quadratic equation. Recall that a qua
ID: 3527836 • Letter: W
Question
Write a complete C++ program that solves a quadratic equation. Recall that a quadratic equation is of the form a*x^2 + b*x + c = 0, and its solution has two roots defined by x1 = (-b + sqrt(b^2 - 4ac)) / 2a and x2 = (-b - sqrt(b^2 - 4ac)) / 2a In your program, define 3 variables named a, b, and c, and declare them of type double. Then, input from the console the values of a, b, and c into these variables. Compute x1 and x2, and then output these values to the console --- output x1 on a line by itself, followed by x2 on a line by itself. Assume the discriminant b^2 - 4ac is always positive. For example, if the values of a, b, and c are 5.0, 6.0, and 1.0, respectively, your program should output -0.2 -1 Instructor's notes: To compute square root, use the builtin sqrt(...) function as shown in the definition of x1 and x2. However, note that C++ does not support raising a variable to a power, so b^2 does not work. Instead do b*b. Likewise, you cannot write 4ac in C++, but instead you have to write 4*a*c. [ HINT: this implies 2a must be written 2*a, which in turn implies you might need to do (2 * a) to get the correct result. ]Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float a,b,c;
cout<<"Enter value a:";
cin>>a;
cout<<"Enter value b:";
cin>>b;
cout<<"Enter value c:";
cin>>c;
float differential = (pow(b,2)-4*a*c);
float root_positif = (((-b)+sqrt(differential))/(2*a));
float root_negatif = (((-b)-sqrt(differential))/(2*a));
if(differential==0)
{
cout<<"The differential is "<<endl;
cout<<differential<<endl;
cout<<"The equation are single root."<<endl;
}
else if(differential<0)
{
cout<<"The differential is"<<endl;
cout<<differential<<endl;
cout<<"The equation are two complex root."<<endl;
}
else
{
cout<<"The differential is"<<endl;
cout<<differential<<endl;
cout<<"The equation are two real root."<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.