Write a complete C++ program that inputs 3 real numbers denoting the coefficient
ID: 3879372 • Letter: W
Question
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++end): 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<math.h>
using namespace std;
int main()
{
double a,b,c;
//reading inuts...
cout<<"Input 3 numbers(a,b,c of ax^2+bx+c):";
cin>>a;
cin>>b;
cin>>c;
//finding discriminant ....
double d = b*b - 4*a*c,r1,r2;
if(d==0)//then it have only single root
{
r1=-1*b/(2*a);
cout<<"one real root: "<<r1<<" "<<endl;
}
else if(d<0)
{
cout<<"No real roots"<<endl;
}
else
{
r1 = ((-1*b)+sqrt(d))/(2*a);
r2 = ((-1*b)-sqrt(d))/(2*a);
cout<<r1<<", "<<r2<<endl;
}
return 0;
}
//output:
/*
Input 3 numbers(a,b,c of ax^2+bx+c):1 3.3 2.0
-0.8, -2.5
Process exited normally.
Press any key to continue . . .
Input 3 numbers(a,b,c of ax^2+bx+c):2 4 2
one real root: -1
Process exited normally.
Press any key to continue . . .
Input 3 numbers(a,b,c of ax^2+bx+c):1 2 3
No real roots
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.