Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Java Compute Sum and Average of Array Elements

class Main {
 public static void main(String[] args) {
   int[] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12};
   int sum = 0;
   Double average;
      // access all elements using for each loop
   // add each element in sum
   for (int number: numbers) {
     sum += number;
   }
     // get the total number of elements
   int arrayLength = numbers.length;
   // calculate the average
   // convert the average from int to double
   average =  ((double)sum / (double)arrayLength);
   System.out.println("Sum = " + sum);
   System.out.println("Average = " + average);
 }
}
 
PREVIOUS NEXT
Tagged: #Java #Compute #Sum #Average #Array #Elements
ADD COMMENT
Topic
Name
1+9 =