-
Notifications
You must be signed in to change notification settings - Fork 11
/
QuadraticEquationRoots.java
38 lines (31 loc) · 1.14 KB
/
QuadraticEquationRoots.java
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
package Problems_On_Number;
public class QuadraticEquationRoots {
static void Roots(int a, int b, int c) {
if (a == 0) {
System.out.println("Invalid");
return;
}
int d = b * b - 4 * a * c;
double sqrt_val = Math.sqrt(Math.abs(d));
if (d > 0) {
System.out.println("Roots are real and different ");
double root1 = (double) (-b + sqrt_val) / (2 * a);
double root2 = (double) (-b - sqrt_val) / (2 * a);
System.out.println(root1 + "\n" + root2);
} else if (d == 0) {
System.out.println("Roots are real and same ");
double root1 = -(double) b / (2 * a);
double root2 = -(double) b / (2 * a);
System.out.println(root1 + "\n" + root2);
} else // d < 0
{
System.out.println("Roots are complex ");
System.out.println(-(double) b / (2 * a) + " + i" + sqrt_val + "\n" +
-(double) b / (2 * a) + " - i" + sqrt_val);
}
}
public static void main(String args[]) {
int a = 1, b = -3, c = -10;
Roots(a, b, c);
}
}