How do you find the sum of all the numbers in a java array


Following program print the sum of the all the numbers in an array.

Example

public class Tester {
   public static void main(String[] args) {
      int[] dataArray = {1, 2, 3, 4};
      int sum = 0;
      for(int i: dataArray) {
         sum += i;
      }
      System.out.println(sum);
   }
}

Output

10

Arushi
Arushi

e

Updated on: 24-Feb-2020

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements