Calculate and print the sum of elements in an array using java

public class ArraySumCalculator {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int sum = calculateArraySum(numbers);
        System.out.println("Sum of array elements: " + sum);
    }

    private static int calculateArraySum(int[] arr) {
        int sum = 0;
        for (int num : arr) {
            sum += num;
        }
        return sum;
    }
}

 

Post your Answer