Home >>Java Programs >Java Program to print the sum of all the items of the array
In this example, we will create a java program to calculate the sum of all the elements of an array. This can be achieved by looping through the given array and add the value of the element in each iteration to any variable sum.
public class Main
{
public static void main(String[] args)
{
int [] arr = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9};
int sum = 0;
for (int i = 0; i < arr.length; i++)
{
sum = sum + arr[i];
}
System.out.println("Sum of all the elements of an array: " + sum);
}
}