Question

In: Computer Science

use Java The two roots of a quadratic equation ax^2 + bx + c = 0...

use Java



The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula:

r1 = (-b + sqrt(b^2 - 4ac)) / (2a)
and
r2 = (-b - sqrt(b^2 - 4ac)) / (2a)

b^2 - 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 0, display one root.
Otherwise, display “The equation has no real roots”.

Note that you can use Math.pow(x, 0.5) to compute sqrt(x).

Sample Run 1

Enter a, b, c: 1.0 3 1
The equation has two roots -0.381966 and -2.61803

Sample Run 2

Enter a, b, c: 1 2.0 1
The equation has one root -1

Sample Run 3

Enter a, b, c: 1 2 3
The equation has no real roots

Class Name: Exercise03_01

If you get a logical or runtime error, please refer https://liveexample.pearsoncmg.com/faq.html.

Solutions

Expert Solution

import java.util.*;
public class Exercise03_01
{ public static void find_root(double a,double b,double c){
    double r1, r2;

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

        // if the determinant is positive
        //display two roots
        if(determinant > 0) {
            r1 = (-b + Math.sqrt(determinant)) / (2 * a);
            r2 = (-b - Math.sqrt(determinant)) / (2 * a);
        System.out.format("The equation has two roots %.6f  and  %.5f", r1,r2);

         }
        // if determinant is 0 
        // display one root
        else if(determinant == 0) {
            r1 = r2 = -b / (2 * a);
              System.out.format("The equation has one root %.0f", r1);
          }
        // If roots are not real
        //dispaly no real roots
        else {
           
            System.out.println("The equation has no real roots");
        }
}
        public static void main(String[] args) {
            Scanner s=new Scanner(System.in);
            
                System.out.print("Enter a,b,c: ");
                double a=s.nextDouble();
                double b=s.nextDouble();
                double c=s.nextDouble();
                find_root(a,b,c);
                
                
                
        }
}


Related Solutions

Write a program usingif-elseif-else statements to calculate the real roots of a quadratic equation ax^2+bx+c=0
Write a program usingif-elseif-else statements to calculate the real roots of a quadratic equation ax^2+bx+c=0
Using excel UserForm construct a Flowchart that solves a quadratic equation ax^2+bx+c=0 for changingvalues of a,...
Using excel UserForm construct a Flowchart that solves a quadratic equation ax^2+bx+c=0 for changingvalues of a, b and c. Please also display the code you have used. Please use excel UserForm Thanks
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c:...
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c: You should provide the following methods (1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c...
1. Solve quadratic equation Ax^2+Bx+C=0 using the quadratic formula x = (-B+ and - sqrt(B^2-4ac)) /...
1. Solve quadratic equation Ax^2+Bx+C=0 using the quadratic formula x = (-B+ and - sqrt(B^2-4ac)) / 2a) and output the two solution with clear explanation could you please do it in MATLAB
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 +...
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 + bx + c = 0 where coefficient a is not equal to 0. The equation has two real roots if its discriminator d = b2 – 4ac is greater or equal to zero. The program gets the three coefficients a, b, and c, computes and displays the two real roots (if any). It first gets and tests a value for the coefficient a. if...
1). Consider the quadratic equation x^2+ 100 x + 1 = 0 (i) Compute approximate roots...
1). Consider the quadratic equation x^2+ 100 x + 1 = 0 (i) Compute approximate roots by solving x^2 -100 x = 0 (ii) Use the quadratic formula to compute the roots of equation (iii) Repeat the computation of the roots but use 3 digit precision. (iv) Compute the relative absolute errors in the two 3 digit precision root approximations in (iii). (v) With x1 =1/2a (-b + sqrt b^2 - 4ac and x2 = 1/2a (-b + sqrt b^2...
Use a system of equations to find the parabola of the form y=ax^2+bx+c that goes through...
Use a system of equations to find the parabola of the form y=ax^2+bx+c that goes through the three given points. (2,-12)(-4,-60)(-3,-37)
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d...
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d ER, intersect at 2 points P and Q. These points are also two points of tangency for the two tangent lines drawn from point A(2,9) upon the parobala. The graph of the cubic function has a y-intercept at (0,-1) and an x intercept at (-4,0). What is the standard equation of the tangent line AP.
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d...
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d ER, intersect at 2 points P and Q. These points are also two points of tangency for the two tangent lines drawn from point A(2,9) upon the parobala. The graph of the cubic function has a y-intercept at (0,-1) and an x intercept at (-4,0). What is the value of the coefficient "b" in the equation of the given cubic function.
Write a program to input the coefficients of a quadratic equation and solve roots for all...
Write a program to input the coefficients of a quadratic equation and solve roots for all cases (including complex roots). VBA. x=(b ^2) - (4ac) I have it for 2 complex and 2 real and repeated. Is that all cases?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT