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
- Initialize sum to 0
- Add each element to sum
- Use for-each or regular for loop
Related Arrays exercises
- Practice Declare and Print Array in Java
- Practice Find Maximum in Java
- Practice Calculate Average in Java
Practice all Arrays exercises · Run this idea in the Java compiler