Question
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,
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: Image result for 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, rounded to 3 decimal places. 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. Print both roots rounded to 3 decimal places using printf(). 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.
6,8c6,9
< Discriminant is 133.85 <
x = 5.767339018856537 <
x = -0.01733901885653788
--- >
Discriminant is 133.850 >
The roots are real numbers: >
x = -0.017 >
x = 5.767
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started