Finding Type and Value of Roots of Quadratic Equation

08:19 0 Comments A+ a-

Problem:
Write a Program that calculates the roots of a quadratic equation. For this, you are required to get the values of a, b and c from user and use quadratic formula for finding the roots. Your program should also display whether the roots are real or non-real.
 If Discriminant is positive then roots are real. 
 If Discriminant is negative then roots are non-real.
Solution:
# include <iostream>
# include <cmath>
using namespace std;
int main()
{
double a,b,c,x,y,z;
double root1;
double root2;

cout<<"Enter a:"<<endl;
cin>>a;
cout<<"Enter b:"<<endl;
cin>>b;
cout<<"Enter c:"<<endl;
cin>>c;
x=((pow(b,2))-4*a*c);
if(x>0)
{
cout<<"Roots are real"<<endl;
root1=(-b+(sqrt(x))/(2*a));
root2=(-b-(sqrt(x))/(2*a));
cout<<"Roots from the quadratic formula are:"<<endl;
cout<<root1<<endl;
cout<<root2<<endl;
}
else
cout<<"Roots are Non-Real"<<endl;

return 0;

}