-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMInMax.java
More file actions
35 lines (30 loc) · 1 KB
/
MInMax.java
File metadata and controls
35 lines (30 loc) · 1 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
public class MInMax {
public static void main(String[] args){
// Declare an array
int[] arr = {75, 65, 24, 11, 45, 32, 22, 3, 54, 5, 7, 60, 0, 20, 66};
// Call maxMin method to find and print minimum and maximum element
maxMin(arr);
}
public static void maxMin(int[] arr) {
// Check if the array is empty
if (arr == null || arr.length == 0){
System.out.println("Array is empty");
return;
}
// intialize variables to store for Maximum and minimum element
int min = arr[0];
int max = arr[0];
// Iterate throught the array to find the minimum and maximum element
for(int i = 1; i < arr.length; i++){
if (arr[i] < min){
min = arr[i];
}
if (arr[i]> max){
max = arr[i];
}
}
// Print the min and max elements
System.out.println("Min: "+min);
System.out.println("Max: "+max);
}
}