#6) The roots of the quadratic equation ax2+ bx + c = 0, a? 0 are give by the fo
ID: 3629904 • Letter: #
Question
#6) The roots of the quadratic equation ax2+ bx + c = 0, a? 0 are give by the following formula: -b +- Sqaure root (b^2 - 4ac)/2a.In this formula, the term b^2 -4ac is called the discriminant. If b^2 - 4ac = 0, then the equation has a single (repeated) root. If b^2 - 4ac > 0, the equation has two real roots. If b^2-4ac < 0, the equation has two complex roots. Write a program that prompts the user to input the value of a (the coefficient of x^2), b (the coefficient of x), and c (the constant term), and outputs the type of roots of the equation. Furthermore, if b^2 - 4ac > or = to 0, the program should output the roots of the quadratics equation. (Hint: Use the method pow or squrt from the class math to calculate the square root. Chapter 3 explain how to use these methods.)
The problem # 6, Page 221, book is (Java from problem analysis to program design), Author is D.S. Malik, Fourth edition.
Explanation / Answer
import System.util.*;
class test
{
public static void main(String[] args)
{
Scanner in =new Scanner(System.in);
double a, b, c, discr, root1, root2;
System.out.println("Enter values of coefficients a,b,c ");
a = in.nextDouble();
b = in.nextDouble();
c = in.nextDouble();
discr = Math.sqrt((b * b) - (4 * a * c));
if(discr > 0)
{
System.out.println("Equation has 2 roots");
root1 = (-b + discr)/2 * a;
root2 = (-b - discr)/2 * a;
System.out.println("First root = " + root1);
System.out.println("Second roor = " + root2);
}
if(discr == 0)
{
System.out.println("Equation has 1 repeated root");
root1 = (-b + discr)/2 * a;
System.out.println("Root = " + root1);
}
if(discr < 0)
System.out.println("Equation has imaginary roots");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.