7)Practice further programs on the usage of arrays in java

PHOTO EMBED

Mon Jul 08 2024 06:04:16 GMT+0000 (Coordinated Universal Time)

Saved by @varuntej #java

public class ArrayOperations {
    public static void main(String[] args) {
        // Initializing an array
        int[] numbers = { 5, 12, 8, 3, 15 };

        // Accessing elements
        System.out.println("Element at index 2: " + numbers[2]);

        // Finding the length of the array
        System.out.println("Length of the array: " + numbers.length);

        // Modifying an element
        numbers[1] = 20;

        // Printing the array
        System.out.print("Modified array: ");
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i] + " ");
        }
        System.out.println();

        // Finding the maximum element
        int maxElement = numbers[0];
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > maxElement) {
                maxElement = numbers[i];
            }
        }
        System.out.println("Maximum element: " + maxElement);

        // Finding the minimum element
        int minElement = numbers[0];
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] < minElement) {
                minElement = numbers[i];
            }
        }
        System.out.println("Minimum element: " + minElement);

        // Iterating through the array to calculate the sum
        int sum = 0;
        for (int num : numbers) {
            sum += num;
        }
        System.out.println("Sum of elements: " + sum);
    }
}
content_copyCOPY