write a program, which calculates and prints the roots of a given quadratic equa
ID: 3630431 • Letter: W
Question
write a program, which calculates and prints the roots of a given quadratic equation, a*x*x+b*x+c=0. all the variables are type double. as input, ask for a,b,and c. calculate the discriminant, d=b*b-4*a*c.if d is less than 0, print "the roots are complex,"and stop.
if d is greater or equal to 0, calculate the roots using the quadratic formula. print the roots with 4 place decimals. call the roots x1 and x2.
surround this program with a while or do while loop so that it can be repeated with different coefficient without using the run command
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a,b,c;
cout << " Enter Values of a , b and c ";
cin >> a>>b>>c;
if(a==0)
{
cout<< " a cannot be zero making it 1" <<endl;
a = 1;
}
if(b*b-4*a*c <0)
cout <<"the Polynomial "<<a <<"*x^2+"<<b<<"x+"<<c << "no real roots "<<endl;
else if(b*b-4*a*c==0)
{
double x = -b/2*a ;
cout <<"the Polynomial "<<a <<"*x^2+"<<b<<"x+"<<c << "has one root :" << x <<endl;
}
else if(b*b-4*a*c>0)
{
double x = (-b + sqrt(b*b-4*a*c))/(2*a) ;
double y = (-b - sqrt(b*b-4*a*c))/(2*a) ;
cout <<"the Polynomial "<<a <<"*x^2+"<<b<<"x+"<<c << "has two roots :" << x << "and " << y<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.