Home / Programs / Write a Java program to find the position (index) of the largest element in an array.
Programming Example

Write a Java program to find the position (index) of the largest element in an array.

👁 21 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:

  int[] arr = {45, 12, 89, 34, 67};

Expected Output:

Largest element: 89
Position (index): 2

Program Code

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);
    }
}

Explanation

The program compares all elements and keeps track of the largest value and its index.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.