How would I write a c++ program from this following word problem? 3. Use the pow() and sqrt() functions to compute the roots of a quadratic equation. Enter the three coefficients with a single input statement. Warning: if you give sqrt() a negative value, the function cannot produce a valid answer.
#include <iostream> #include <cmath> using namespace std; int main(int argc, char* argv[]) { double a, b, c, root1, root2, d; cout << "Enter quadratic coefficients a,b,c: "; cin >> a; cin >> b; cin >> c; d = b * b - 4.0 * a * c; if (d < 0) { cout << " no real roots\n"; } else if (d == 0) { root1 = -b / (2.0 * a); cout<<"Roots are "<<root1<<" and "<<root1<<endl; } else if (d > 0) { root1 = (-b + sqrt(d)) / (2.0 * a); root2 = (-b - sqrt(d)) / (2.0 * a); cout<<"Roots are "<<root1<<" and "<<root2<<endl; } return 0; }
Get Answers For Free
Most questions answered within 1 hours.