Sum of Array in Java: Explanation & Practice

Calculate the sum of array elements

Problem summary

Find the sum of all elements in the array [5, 10, 15, 20, 25].

Starter code

public class Main {
    public static void main(String[] args) {
        int[] numbers = {5, 10, 15, 20, 25};
        
        // Calculate and print sum
        // Print: Sum: <result>
    }
}

Expected output and test cases

  • Sum of array
    Sum: 75

Hints

  1. Initialize sum to 0
  2. Add each element to sum
  3. Use for-each or regular for loop

Related Arrays exercises

Practice all Arrays exercises · Run this idea in the Java compiler