-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.java
More file actions
30 lines (26 loc) · 1.02 KB
/
array.java
File metadata and controls
30 lines (26 loc) · 1.02 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
// Write a program to make an array and find max. and min. element of the array and also find the index of the element
public class array {
public static void main(String[] args) {
int[] array = { 5, 2, 8, 12, 3 }; // Initialize array
int max = array[0]; // Initialize max and min with first element
int min = array[0];
int maxIndex = 0;
int minIndex = 0;
// Loop through array to find max, min and their indices
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
maxIndex = i;
}
if (array[i] < min) {
min = array[i];
minIndex = i;
}
}
// Print results
System.out.println("Max element: " + max);
System.out.println("Index of Max element: " + maxIndex);
System.out.println("Min element: " + min);
System.out.println("Index of Min element: " + minIndex);
}
}