-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.8 A
32 lines (24 loc) · 1.06 KB
/
2.8 A
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*; //Allows use of Scanner
public class SolveQuadratic
{
public static void main(String args[])
{
int a, b, c; //ax^2 + bx + c
double discriminant; // b^2 - 4ac
double root1, root2; // root1 for add, root2 for subtract
Scanner keyboard = new Scanner(System.in); // Creates an object for inputs
System.out.print ("Enter the coefficient of x^2: " );
a = keyboard.nextInt();
System.out.print ("Enter the coefficient of x: ");
b = keyboard.nextInt();
System.out.print ("Enter the constant: ");
c = keyboard.nextInt();
//use the quadratic formula to compute the roots
//assumes a positive discriminant
discriminant = Math.pow(b,2) - (4 * a * c);
root1 = ((-1 * b) + Math.sqrt(discriminant)) / (2 * a);
root2 = ((-1 * b) - Math.sqrt(discriminant)) / (2 * a);
System.out.println ("Root #1: " + root1);
System.out.println ("Root #2: " + root2);
} //end main
} //end class