Write the equivalent Java Expression of the following formulas [2]
x=(-b±√(b^2-4ac))/2a
Answers
Answer:
Roots of a quadratic equation are determined by the following formula:
x=−b±b2−4ac‾‾‾‾‾‾‾‾√2a
x
=
−
b
±
b
2
−
4
a
c
2
a
To calculate the roots −
Calculate the determinant value (b*b)-(4*a*c).
If determinant is greater than 0 roots are [-b +squareroot(determinant)]/2*a and [-b -squareroot(determinant)]/2*a.
If determinant is equal to 0 root value is (-b+Math.sqrt(d))/(2*a)
Example
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.04455555833347
Explanation:
Explanation:
equivalent java expression:
x=(-b+Math.sqrt(b*b-4*a*c))/2*a)