Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

JAVA Sum of Array Elements

// Calculate the sum of all elements of an array

class Main {
 public static void main(String[] args) {
  
   // an array of numbers
   int[] numbers = {3, 4, 5, -5, 0, 12};
   int sum = 0;

   // iterating through each element of the array 
   for (int number: numbers) {
     sum += number;
   }
  
   System.out.println("Sum = " + sum);
 }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #JAVA #Sum #Array #Elements
ADD COMMENT
Topic
Name
9+4 =