C++ Program to solve Quadratic Equation.

#include<iostream>
#include<math.h>

using namespace std;

main()
{
float a,b,c,f,g,h;

cout<<"Enter the value of a =";
cin>>a;
cout<<"Enter the value of b =";
cin>>b;
cout<<"Enter the value of c =";
cin>>c;

h=(b*b)-(4*a*c);
f=-b+sqrt(h)/2*a;
g=-b-sqrt(h)/2*a;

    if(h<0)
    {
cout<<"\nRoots are imaginary.";
}

else if (h>0)
{
cout<<"\nThe first root = "<<f<<endl;
cout<<"The second root = "<<g;
}

else if (h==0)
{
cout<<"\nThe roots are equal."<<endl;
cout<<"\nThe first root = "<<f<<endl;
cout<<"\nThe second root = "<<g;
}
}

Output:



No comments:

Post a Comment