Java program to sum all the elements of an Array

Views: 148   

Java program that loop over an array of numbers and calculate the sum of all those numbers

public class ArrayElementsSum {

    public static void main(String[] args) {

          int[] array = {10, 20, 30, 40, 50, 20};
          int result = 0;

          for( int i : array) {
              result += i;
          }

          System.out.println("Sum of all elements is:" + result);    
    }
}

Output:

Sum of all elements is:170
On By