➽ Program:-
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float a, b, c,
discriminant, Root1, Root2, realPart, imaginaryPart;
cout<<"Enter coefficients(a,b,c): ";
cin>> a
>> b >> c;
discriminant = b * b
- 4 * a * c;
// For
real and different roots
if(discriminant >
0)
{
Root1 = (-b + sqrt(discriminant)) / (2*a);
Root2 = (-b - sqrt(discriminant)) / (2*a);
cout<<"Roots are real and different"<< endl;
cout<<"Root1 = "<< Root1 << endl;
cout<<"Root2 = "<< Root2 << endl;
}
// For
real and equal roots
else if(discriminant
== 0)
{
cout<<"Roots are real and same"<< endl;
Root1 = -b/(2*a);
cout<<"Root1 = Root2 = "<< Root1 << endl;
}
// For
non real roots
else
{
realPart = -b/(2*a);
imaginaryPart = sqrt(-discriminant)/(2*a);
cout<<"Roots are complex and different"<< endl;
cout<<"Root1 = "<< realPart << "+"
<< imaginaryPart << "i" << endl;
cout<<"Root2 = "<< realPart << "-"
<< imaginaryPart << "i" << endl;
}
return 0;
}
➽ Output:-
0 Comments
Please do not enter any spam link in the comment section.