-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadratic.java
More file actions
36 lines (34 loc) · 1.29 KB
/
Quadratic.java
File metadata and controls
36 lines (34 loc) · 1.29 KB
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
33
34
35
36
/**
* @author Brock Francom
* A02052161
* Quadratic
* This computes the roots of a quadratic by entering the leading coefficients of each term.
*/
import java.util.Scanner;
public class Quadratic {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a, b, c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
// computes the discriminant
double discriminant = (b * b) - (4 * a * c);
// computes the roots
double r1 = (- b + Math.pow(discriminant, .5)) / (2 * a);
double r2 = (- b - Math.pow(discriminant, .5)) / (2 * a);
// prints out a message
if (discriminant > 0) {
System.out.println("There are two roots for the quadratic equation with these coefficients.");
System.out.println("r1 = " + r1);
System.out.println("r2 = "+ r2);
}
else if (discriminant == 0) {
System.out.println("There is one root for the quadratic equation with these coefficients.");
System.out.println("r1 = " + r1);
}
else if (discriminant < 0) {
System.out.println("There are no roots for the quadratic equation with these coefficients.");
}
}
}