-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaiTapGiaiPhuongTrinhBacHai.java
47 lines (40 loc) · 1.42 KB
/
BaiTapGiaiPhuongTrinhBacHai.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
39
40
41
42
43
44
45
46
47
import java.util.Scanner;
public class BaiTapGiaiPhuongTrinhBacHai {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("nhap so a: ");
double a = scanner.nextDouble();
System.out.print("nhap so b: ");
double b = scanner.nextDouble();
System.out.print("nhap so c: ");
double c = scanner.nextDouble();
if (a == 0) {
// Giải phương trình bậc nhất
if (b == 0) {
if (c == 0) {
System.out.println("phuong trinh vo so nghiem");
} else {
System.out.println("phuong trinh vo nghiem.");
}
} else {
double x = -c / b;
System.out.println("Nghiem cua phuong trinh la x = " + x);
}
} else {
double delta = b * b - 4 * a * c;
if (delta < 0) {
System.out.println("Phuong trinh vo nghiem.");
} else if (delta == 0) {
double x = -b / (2 * a);
System.out.println("Phuong trinh co nghiem kep x = " + x);
} else {
double x1 = (-b + Math.sqrt(delta)) / (2 * a);
double x2 = (-b - Math.sqrt(delta)) / (2 * a);
System.out.println("Phuong trinh co hai nghiem phan biet:");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}
}
scanner.close();
}
}