-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathRootsOfQuadraticEqn.java
More file actions
47 lines (40 loc) · 1.33 KB
/
Copy pathRootsOfQuadraticEqn.java
File metadata and controls
47 lines (40 loc) · 1.33 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
37
38
39
40
41
42
43
44
45
46
47
//Find the roots of the quadratic Equation
import java.util.Scanner;
public class RootsOfQuadraticEqn {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
double a = 0;
double b = 0;
double c = 0;
double root1, root2;
System.out.println("Quadratic Equation : a(x)^2 + b(x) + c = 0");
System.out.println("Enter value of a: ");
a = reader.nextDouble();
System.out.println("Enter value of b: ");
b = reader.nextDouble();
System.out.println("Enter value of c: ");
c = reader.nextDouble();
// d stands for determinant
double d = (b * b) - 4 * (a * c);
//if d = 0 roots are equal and real, id d>0 roots are real and distinct
if (d >= 0) {
root1 = (-b + Math.sqrt(d)) / (2 * a);
root2 = (-b - Math.sqrt(d)) / (2 * a);
System.out.println("root1 = "+root1+ "\nroot2 = "+ root2);
if (root1 == root2) {
System.out.println("Both roots are equal and real!");
}
else{
System.out.println("Roots are real and distinct");
}
}
//d<0, roots are imaginary
else {
double real = -b / (2 * a);
double imaginary = Math.sqrt(-d) / (2 * a);
System.out.println("root1 = "+real+ " + " +imaginary+"i");
System.out.println("root2 = "+real+ " - " +imaginary+"i");
System.out.println("Roots are imaginary!");
}
}
}