Respuesta :
Answer:
// here is code in c++.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// variables
int a,b,c;
float d;
cout<<"enter the value of a,b,c (separated by space):";
// read the value of a,b,c
cin>>a>>b>>c;
// calculate discriminant
d=b*b-(4*a*c);
// if d<0, then no read root
if(d<0)
{
cout<<"The equation has no real roots"<<endl;
}
// d=0, then one real root
else if(d==0)
{
float r=-b/(2*a);
cout<<"only root of the equation is: "<<r<<endl;
}
// d>=0 then two real roots
else if(d>0)
{
// calculate the roots
float r1=(-b+sqrt(d))/(2*a);
float r2=(-b-sqrt(d))/(2*a);
// print the roots
cout<<"first root of the equation is: "<<r1<<endl;
cout<<"second root of the equation is: "<<r2<<endl;
}
return 0;
}
Explanation:
Read the value of a,b,c from user.Calculate discriminant "d" as b^2 - 4ac . If the value of discriminant is less than 0 then there will be no real root. If discriminant is equal to 0 then quadratic equation has only one root which is -b/2a. Calculate the only root and print it.If discriminant is greater than 0 then quadratic equation has two root as r1 = (-b + sqrt(b^2 - 4ac)) / (2a) and r2 = (-b - sqrt(b^2 - 4ac)) / (2a). calculate both the root and print them.
Output:
enter the value of a,b,c (separated by space): 2 4 1
first root of the equation is: -0.292893
second root of the equation is: -1.70711