JAVA Programming....( Algebra: solve quadratic equations ) The two roots of a qu
ID: 655623 • Letter: J
Question
JAVA Programming....(Algebra: solve quadratic equations) The two roots of a quadratic equation ax2 + bx + c = 0
can be obtained using the following formula: (Quadratic forumula)
r1 = -b+ sqaure root of b^2 - 4ac / 2a and r2 = -b+ sqaure root of b^2 - 4ac / 2a
The Sqaure root of 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
Explanation / Answer
import java.util.Scanner;
public class equation {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("enter a,b and c value");
int a=scan.nextInt();
int b=scan.nextInt();
int c=scan.nextInt();
double temp = b * b - 4 * a * c;
double temp1=Math.pow(temp, 0.5);
double root1 = (-b + temp1) / (2*a) ;
double root2 = (-b - temp1) / (2*a) ;
if(temp==0)
{
System.out.println("the equation has one roots");
System.out.println(root1);
}
else if(temp<0)
{
System.out.println("the equation has no real roots");
}
else
{
System.out.println("the equation has two real roots");
System.out.println("the roots are: "+root1+" & "+root2);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.