Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Sum of Array Elements JAVA

// 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: #Sum #Array #Elements #JAVA
ADD COMMENT
Topic
Name
8+2 =