Solve Quadratic equation :-

<br />#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float a,b,c;
float d,f_root,s_root;
cout<<"Enter a, b and c of quadratic equation:"<<endl;;
cin>>a>>b>>c;
cout<<endl<<"Quadratic equation is :-"<<endl<<a<<"x^2 + "<<b<<"x + "<<c<<" "<<endl;

d = b * b - 4 * a * c;

if(d < 0)
{
cout<<"Roots are complex number."<<endl;

cout<<"Roots of quadratic equation are:"<<endl;
cout<<-b/(2*a)<<"+"<<sqrt(-d)/(2*a)<<"i"<<endl;
cout<<-b/(2*a)<<"+"<<-sqrt(-d)/(2*a)<<"i";

return 0;
}
else if(d==0)
{
cout<<"Both roots are equal."<<endl;

f_root = -b /(2* a);
cout<<"Root of quadratic equation is:"<<f_root;

return 0;
}
else
{
cout<<"Roots are real numbers."<<endl;

f_root = ( -b + sqrt(d)) / (2* a);
s_root = ( -b - sqrt(d)) / (2* a);
cout<<"Roots of quadratic equation are: "<<f_root<<" & "<<s_root;
}

return 0;
}

Output :-
Untitled

Leave a comment