Given an array of ‘N’ elements .Return the sum of all array elements





Test cases

Test cases Expected Result
arraySum({5,5,5,5,5}) 25 OK
arraySum({1,3,4,7,8}) 23 OK
arraySum({100,200,300}) 600 OK

Java solution:

public class Program8 {
	public static void main(String[] args) {
		int a[] = {5,5,5,5,5};
		int b[] = {1,3,4,7,8};
		int c[] = {100,200,300};
		Program8.arraySum(a);
		Program8.arraySum(b);
		Program8.arraySum(c);
	}
	public static long arraySum(int[] a){
		int sum = 0;
		for (int i = 0; i < a.length; i++) {
			sum += a[i];
		}
		return sum;
	}
}

Javascript solution:

const arraySum = (array) => {
    let sum = 0;
    for (let index = 0; index < array.length; index++) {
        sum += array[index];
    }
    return sum;
}

console.log(arraySum([5,5,5,5,5])); //25
console.log(arraySum([1,3,4,7,8])); //23
console.log(arraySum([100,200,300])); //600




Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply