Java Code Example: sum up all numbers from an array

This Java program is used to find the sum of all elements in an array. The program defines an array named numbers with 9 elements.

In the main method, a variable sum is initialized to 0. A for loop is used to iterate through the elements of the numbers array. In each iteration of the loop, the value of numbers[i] is added to the sum variable.

Finally, the sum of all elements in the array is displayed by printing "Sum of all elements in the array is: " + sum.

import java.util.Scanner;

public class SumUpNumbers {
    public static void main(String[] args) {
        int numbers[] = { 65, 34, 23, 75, 76, 33, 12, 9, 44 };

        int sum = 0;

        for (int i = 0; i < numbers.length; i++) {
            sum += numbers[i];
        }

        System.out.println("Sum of all elements in the array is: " + sum);
    }
}
Output
Sum of all elements in the array is: 371