3.1 (Algebra: solve quadratic equations) The two roots of a quadratic equation a
ID: 3795447 • Letter: 3
Question
3.1 (Algebra: solve quadratic equations) The two roots of a quadratic equation ax bx.c 0can be obtained using the following formula: 4ac and b -4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots. write a program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 8, display one root. display "The equation has no real roots". otherwise, Note that you can use Math pow(x. 5) to VT. Here is a sample run. 11.5 45.2 12.4 Show the sample output Using the Preceeding Input and Java Exercise 3 ei rnter a, b. 13.5 47.2 12.4. The uation two rootsExplanation / Answer
// SolveQuadraticEquation.java
import java.util.Scanner;
public class SolveQuadraticEquation
{
public static void main(String[] args)
{
double a, b, c;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a, b, c: ");
a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();
double discriminant, root1, root2;
discriminant = (b*b - 4*a*c);
if(discriminant < 0)
{
System.out.println("The equation has no real roots");
}
else if(discriminant == 0)
{
root1 = (-b+Math.sqrt(discriminant))/(2*a);
root2 = (-b-Math.sqrt(discriminant))/(2*a);
System.out.println("The equation has two equal roots " + root1 + " and " + root2);
}
else
{
root1 = (-b+Math.sqrt(discriminant))/(2*a);
root2 = (-b-Math.sqrt(discriminant))/(2*a);
System.out.println("The equation has two roots " + root1 + " and " + root2);
}
}
}
/*
output:
Enter a, b, c: 13.5 45.2 12.4
The equation has two roots -0.3014832803673518 and -3.046664867780797
Enter a, b, c: 1 2 3
The equation has no real roots
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.