In: Computer Science
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 + bx + c = 0
where coefficient a is not equal to 0. The equation has two real roots if its discriminator d = b2
– 4ac is greater or equal to zero. The program gets the three coefficients a, b, and c, computes
and displays the two real roots (if any). It first gets and tests a value for the coefficient a. if this
value is zero, the program displays an error message and terminates, otherwise it proceeds to
get a value for the coefficient b and a value for the coefficient c. Next it computes and tests the
discriminator d = b^2 – 4ac. If the discriminator d is negative, the program displays an error
message “No Real Roots!!” and terminates, otherwise it proceeds to compute and display the two
real roots R1 and R2 according to the following formulas:
R1 =
−b+√b
2−4ac
2a
and R2 =
−b−√b
2−4ac
2a
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a,b,c,d,r1,r2;
//input coefficient a
cout<<"Enter a: ";
cin>>a;
if(a==0)
{
cout<<"Coefficient a must not be equal to
0"<<endl;
}
else
{
//input coefficients b and c
cout<<"Enter b and c: ";
cin>>b>>c;
//calculate discriminant
d=(b*b)-(4*a*c);
if(d<0) //no real roots
cout<<"No Real Roots!!"<<endl;
else
{
//calculate roots
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
cout<<"Roots are r1= "<<r1<<" and r2=
"<<r2<<endl;
}
}
}
Output