Wednesday 26 October 2016

Sum of Elements


Given an array of  integers, can you find the sum of its elements?

SOURCE CODE:
import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();           //inputs taken from stdin
        int arr[]=new int[n];
        int sum=0;
        for(int i=0;i<n;i++)
            {
            arr[i]=sc.nextInt();
            sum+=arr[i];
        }
        System.out.println(sum);
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    }
}

No comments:

Post a Comment