-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimSoLonNhat.java
More file actions
29 lines (25 loc) · 944 Bytes
/
timSoLonNhat.java
File metadata and controls
29 lines (25 loc) · 944 Bytes
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
public class timSoLonNhat {
/**
* Hàm này tìm và trả về số lớn nhất trong một mảng số nguyên.
* @param arr Mảng số nguyên đầu vào.
* @return Số lớn nhất trong mảng.
* @throws IllegalArgumentException nếu mảng rỗng hoặc null.
*/
public static int timSoLonNhat(int[] arr) {
if(arr == null || arr.length == 0){
throw new IllegalArgumentException("mang dang bị null");
}
int soLonNhat = arr[0];
for (int i = 1; i < arr.length; i++) {
if(arr[i] > soLonNhat){
soLonNhat = arr[i] ;
}
}
return soLonNhat;
}
public static void main(String[] args) {
int[] mangSo = {15, 8, 99, 42, 7, 101, 3};
int ketQua = timSoLonNhat(mangSo);
System.out.println("Số lớn nhất trong mảng là: " + ketQua); // Kết quả: 101s
}
}