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 Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 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.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

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.

🔥 Practice suggestion

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.