add all values in array java code example
Example 1: sum numbers in array java
public class Exercise2 {
public static void main(String[] args) {
int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 0;
for (int i : my_array)
sum += i;
System.out.println("The sum is " + sum);
}
}
Example 2: how to add up all numbers in an array
const numbers = [10, 20, 30, 40]
add = (a, b) => a + b
const sum = numbers.reduce(add)