int[] arr = {45, 12, 89, 34, 67};
Largest element: 89 Position (index): 2
class LargestElementPosition {
public static void main(String[] args) {
int[] arr = {45, 12, 89, 34, 67};
int max = arr[0];
int position = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
position = i;
}
}
System.out.println("Largest element: " + max);
System.out.println("Position (index): " + position);
}
}
The program compares all elements and keeps track of the largest value and its index.
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.