Question: DESCRIPTION Create a program that uses the quadratic formula to solve a quadratic equation of the form ax 2 +bx+c = 0, given the coefficients
DESCRIPTION
Create a program that uses the quadratic formula to solve a quadratic equation of the form ax2+bx+c = 0, given the coefficients a, b, and c.
Recall the quadratic formula:
where the "discriminant" of the equation is the portion of the formula under the radical (b2-4ac).
Your program should:
1) Prompt the user to input the coefficients of the quadratic equation (allow decimal values) and print out the equation in standard form: ax^2+bx+c=0
2) Calculate and output the discriminant of the equation, b2-4ac
3) If the discriminant is negative, print a message that the roots (the solutions) are complex (have an imaginary part) and finish the program.
4) If the discriminant is non-negative (greater than or equal to 0) the roots are real numbers, calculate and print the roots. Note: the formula, in general, generates two roots because of the +/- in the numerator. For this program, calculate and print the smaller root first. Hint: one option is to use Math.min() and Math.max() to get the smaller and the larger of the two roots calculated.
You may assume the 'a' coefficient is not zero, so it will be a valid quadratic equation. You may assume all the coefficients input are valid decimal numbers.
Sample output:
Enter the coefficients of the quadratic equation (a,b,c):
a:
2
b:
10
c:
12
Equation: 2.0x^2 + 10.0x + 12.0 = 0
Discriminant is 4.0
The roots are real numbers:
x = -3.0
x = -2.0
Sample output:
Enter the coefficients of the quadratic equation (a,b,c):
a:
1
b:
1.5
c:
1
Equation: 1.0x^2 + 1.5x + 1.0 = 0
Discriminant is -1.75
The roots are complex numbers.
STARTER CODE:
import java.util.Scanner;
public class Quadratic { public static void main(String args[]) { //create a Scanner for keyboard entry and double variables for the coefficients Scanner keyboard = new Scanner(System.in); double a,b,c; //prompt the user for the coefficients of the equation System.out.println("Enter the coefficients of the quadratic equation (a,b,c):");
//print the equation System.out.println("Equation: " + a+"x^2 + "+b+"x + "+c+" = 0"); //compute the discriminant: b^2-4ac double discriminant; System.out.println("Discriminant is " + discriminant); //calculate and print the real-number roots, if the discriminant is non-negative. //otherwise print a message that the roots are complex. } } //end Quadratic
a CStep by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
