Write a C++ program to solve quadratic equations of the form: a*x^2 + b*x + c =
ID: 3623209 • Letter: W
Question
Write a C++ program to solve quadratic equations of the form:
a*x^2 + b*x + c = 0
(where x^2 means x to the second power), for which coefficients a, b, c are entered from the keyboard by an operator, after prompt, as follows:
Enter coefficient a: <operator enters a>
Enter coefficient b: <operator enters b>
Enter coefficient c: <operator enters c>
<one empty line should follow this sequence of three prompts>
After equation is solved, the program should display the results in the following format:
1) If the solutions exist in the real domain, the program should output:
Quadratic equation with the following coefficients:
a: <value>; b: <value>; c: <value>
has the following roots
Root1: <value>; Root2: <value>;
<one empty line should follow this sequence of statements>
2) In case there are no solutions (roots) in the real domain, the program should output:
Quadratic equation with the following coefficients:
a: <value>; b: <value>; c: <value>
has no roots in the real domain.
<one empty line should follow this sequence of statements>
The program should run in a loop, solving one equation in a single iteration of the loop, where the number of iterations of the loop is determined by reading a value from the keyboard at the beginning of the program, before any coefficients are entered, in the following format:
This is a Quadratic Equation Solver.
Enter the number of equations to solve: <operator enters a value>
Explanation / Answer
please rate- thanks
#include<iostream>
#include<cmath>
using namespace std;
int main()
{double a,b,c;
int i,n;
double discriminant,denom;
cout<<"This is a Quadratic Equation Solver. ";
cout<<"Enter the number of equations to solve: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"For the equation ax^2+bx+c: ";
cout<<"Enter coefficient a: ";
cin>>a;
cout<<"Enter coefficient b: ";
cin>>b;
cout<<"Enter coefficient c: ";
cin>>c;
discriminant=pow(b,2.)-4*a*c;
denom=2.*a;
4
cout<<"The roots of "<<a<<"x^2+"<<b<<"x+"<<c<<" are: ";
if(a==0)
cout<<"undefined ";
else if(discriminant<0)
cout<<"imaginary ";
else
cout<<(-b+sqrt(discriminant))/denom<<" and "<<(-b-sqrt(discriminant))/denom<<endl;
cout<<endl;
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.