-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadratic.java
More file actions
50 lines (35 loc) · 1.34 KB
/
Copy pathQuadratic.java
File metadata and controls
50 lines (35 loc) · 1.34 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
48
49
50
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaprograming;
/**
*
* @author rohan
*
If determinant is greater than 0, the roots are real and different.
If determinant is equal to 0, the roots are real and equal.
If determinant is less than 0, the roots are complex and different.
*/
public class Quadratic {
public static void main(String[] args) {
double a = 2.3, b = 4, c = 5.6;
double root1, root2;
double determinant = b * b - 4 * a * c;
if (determinant > 0) {
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
System.out.format("root1 = %.2f and root2 = %.2f;",root1,root2);
}
else if(determinant == 0){
root1 = root2 = -b/(2 * a);
System.out.format("root1 = root2 = %.2f;",root1);
}
else{
double realPart = -b/(2 * a);
double imaginaryPart = Math.sqrt(-determinant)/(2 * a);
System.out.format("root1 = %.2f+5.2fi and root2 = %.2f-%.2fi", realPart,imaginaryPart,realPart,imaginaryPart);
}
}
}