Computing the sum of the elements of an array. - Java Language Basics

Java examples for Language Basics:Array

Description

Computing the sum of the elements of an array.

Demo Code

public class Main 
{
   public static void main(String[] args)
   {/*  ww  w. j  ava 2s .  c o m*/
      int[] array = {817, 681, 94, 110, 831, 178, 185, 191, 76, 87};
      int total = 0;

      // add each element's value to total
      for (int counter = 0; counter < array.length; counter++)
         total += array[counter];

      System.out.printf("Total of array elements: %d%n", total);
   }    
}

Result


Related Tutorials