➽ 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:- 

Enter coefficients(a,b,c): 7
8.1
10.2

Roots are complex and different

Root1 = -0.578571+1.05943i
Root2 = -0.578571-1.05943i