HackerRank.com Challenge – Simple Array Sum

Here is my submission for HackerRank.com’s Simple Array Sum challenge. In it I used a foreach loop to add all of the values located in an array and return the result.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static int simpleArraySum(int n, int[] ar) {
        // Complete this function
        int runningTotal = 0;
        for(int number : ar)
            runningTotal = runningTotal + number;    
        return runningTotal;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] ar = new int[n];
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextInt();
        int result = simpleArraySum(n, ar);

Comments are closed.