➽ Program:-

import java.util.*;

public class Main

{

    public static void main(String args[])

    {

      double a, b, c, discriminant, Root1, Root2, realPart, imaginaryPart;

      Scanner sc = new Scanner(System.in);

     

      System.out.print("Enter the First Coefficient: ");

      a = sc.nextDouble();

      System.out.print("Enter the Second Coefficient: ");

      b = sc.nextDouble();

      System.out.print("Enter the Third Coefficient: ");

      c = sc.nextDouble();

 

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

 

      // For real and different roots

      if(discriminant > 0)

      {

        Root1 = (-b + Math.sqrt(discriminant)) / (2 * a);

        Root2 = (-b - Math.sqrt(discriminant)) / (2 * a);

        System.out.print("Roots are real and different");

        System.out.format("\nRoot1 = %.2f \nRoot2 = %.2f", Root1, Root2);

      }

 

      // For real and equal roots

      else if(discriminant == 0)

      {

        Root1 = Root2 = -b / (2 * a);

        System.out.print("Roots are real and same");

        System.out.format("\nRoot1 = Root2 = %.2f", Root1);

      }

 

      // For non real roots

      else

      {

        realPart = -b / (2 * a);

        imaginaryPart = Math.sqrt(-discriminant) / (2 * a);

        System.out.print("Roots are complex and different");

        System.out.format("\nRoot1 = %.2f+%.2fi", realPart, imaginaryPart);

        System.out.format("\nRoot2 = %.2f-%.2fi", realPart, imaginaryPart);

      }

    }

}

➽ Output:-

Enter the First Coefficient: 7
Enter the Second Coefficient: 8.1
Enter the Third Coefficient: 10.2

Roots are complex and different

Root1 = -0.58+1.06i
Root2 = -0.58-1.06i