...

/

Solution Review: Compute Sum of the Array

Solution Review: Compute Sum of the Array

The solution to the Compute Sum of the Array exercise is explained here.

We'll cover the following...

Coding solution

Let’s take a look at the solution first:

Press + to interact
import java.util.*;
import java.util.Arrays;
public class ArraySum {
public static void addingAColectionOfNumbers(int[] aCollectionOfNumbers){
int sum = 0;
for (int i = 0; i < aCollectionOfNumbers.length; i++){
sum += aCollectionOfNumbers[i];
}
System.out.println("\n\nThe sum is " + sum);
}
public static void main(String[] args){
int[] anotherNumber = {-10, 15, -1, -8};
System.out.print("Array: ");
for (int i:anotherNumber) {
System.out.print(i + " ");
}
addingAColectionOfNumbers(anotherNumber);
}
}

Explanation

Doing any arithmetic operations on any number ...