Computer Science, asked by ghoshdebabrata2004, 8 months ago

Write a program in Java to find the roots of a quadratic equation ax2+bx+c=0.​

Answers

Answered by revankarnayana7
3

Answer:

mark me as brainliest ok

Attachments:
Answered by vijayainesh
8

Input:

import java.util.Scanner;

public class RootsOfQuadraticEquation {

  public static void main(String args[]){

     double secondRoot = 0, firstRoot = 0;

     Scanner sc = new Scanner(System.in);

     System.out.println("Enter the value of a ::");

     double a = sc.nextDouble();

     System.out.println("Enter the value of b ::");

     double b = sc.nextDouble();

     System.out.println("Enter the value of c ::");

     double c = sc.nextDouble();

     double determinant = (b*b)-(4*a*c);

     double sqrt = Math.sqrt(determinant);

     if(determinant>0){

        firstRoot = (-b + sqrt)/(2*a);

        secondRoot = (-b - sqrt)/(2*a);

        System.out.println("Roots are :: "+ firstRoot +" and "+secondRoot);

     }else if(determinant == 0){

        System.out.println("Root is :: "+(-b + sqrt)/(2*a));

     }

  }

}

Output:

Enter the value of a ::

15

Enter the value of b ::

68

Enter the value of c ::

3

Roots are :: -0.044555558333472335 and -4.488777774999861

ANOTHER EXAMPLE:

INPUT:

public class Quadratic {

   public static void main(String[] args) {

       double a = 2.3, b = 4, c = 5.6;

       double root1, root2;

       double determinant = b * b - 4 * a * c;

       // condition for real and different roots

       if(determinant > 0) {

           root1 = (-b + Math.sqrt(determinant)) / (2 * a);

           root2 = (-b - Math.sqrt(determinant)) / (2 * a);

           System.out.format("root1 = %.2f and root2 = %.2f", root1 , root2);

       }

       // Condition for real and equal roots

       else if(determinant == 0) {

           root1 = root2 = -b / (2 * a);

           System.out.format("root1 = root2 = %.2f;", root1);

       }

       // If roots are not real

       else {

           double realPart = -b / (2 *a);

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

           System.out.format("root1 = %.2f+%.2fi and root2 = %.2f-%.2fi", realPart, imaginaryPart, realPart, imaginaryPart);

       }

   }

}

OUTPUT:

root1 = -0.87+1.30i and root2 = -0.87-1.30i

OTHER EXAMPLE

INPUT:

public class QuadraticEquation {

   static void findRoots(int a, int b, int c){

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

       double root_1 = (-b + Math.sqrt(d))/(2*a);

       double root_2 = (-b - Math.sqrt(d))/(2*a);

       System.out.format("Quadratic Equation: %+dx^2 %+dx %+d = 0", a, b, c);

       System.out.println("Roots are: (" + root_1 + ", " + root_2);

   }

   public static void main(String[] args) {

       int a = 3;

       int b = -5;

       int c = -8;

       findRoots(a, b, c);

   }

}

OUTPUT:

Quadratic Equation: +3x^2 -5x -8 = 0, Roots are:(2.6666666666666665, -1.0

HOPE THIS HELPS YOU

HAVE A NICE DAY:)

Similar questions