Find and print the maximum element in an array using java

public class MaxElementFinder {
    public static void main(String[] args) {
        int[] numbers = {4, 7, 2, 9, 1, 5};
        int max = findMaxElement(numbers);
        System.out.println("Maximum element in the array: " + max);
    }

    private static int findMaxElement(int[] arr) {
        int max = arr[0];
        for (int num : arr) {
            if (num > max) {
                max = num;
            }
        }
        return max;
    }
}

 

Post your Answer