2.24 Lab02: Solving Quadratic Equations Write a complete C++ program that inputs
ID: 3743695 • Letter: 2
Question
2.24 Lab02: Solving Quadratic Equations Write a complete C++ program that inputs 3 real numbers denoting the coefficients a, b, and c for the quadratic equation ax^2 + bx-c = 0, solves for x, and outputs the real root solution(s). For example, given the input 1.0 3.3 2.0 Your program should output the 2 real roots followed by a newline (C++ endl) 0.8, -2.5 If the discriminant is 0.0, then there is only one real root, and your program should output exactly one value. For example, given the input 2.0 4.0 2.0 Your program should output one real root: -1 Finally, if the discriminant is negative, then there are no real roots, and your program should say so. For example, given the input 1.0 2.0 3.0 Your program should output no real rootsExplanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a,b,c,x1,x2,D;
cin>>a>>b>>c;
D = b*b-4*a*c;
if (D>0) {
x1=(-b+sqrt(D))/(2*a);
x2=(-b-sqrt(D))/(2*a);
cout<<x1<<", "<<x2;
}
else if(D==0){
x1=(-b+sqrt(D))/(2*a);
cout<<"one real root: "<<x1;
}
else {
cout<<"no real roots";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.