Array Sum | Competitive Programming Questions with Answers in Java

Competitive Programming Questions with Answers in Java

Array Sum

Problem

You are given an array of integers of size . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large.
Input
The first line of the input consists of an integer . The next line contains space-separated integers contained in the array.

Output
Print a single value equal to the sum of the elements in the array.

Constraints
1<=N<=10 0<=a[i]<=10^10


Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005

Sample Output

5000000015

Solution

import java.util.*;


class TestClass {
    public static void main(String args[] ) throws Exception {
        int n, i;
        long sum = 0;
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        long a[] = new long[n];
        for(i=0; i<n; i++)
        {
            a[i] = s.nextLong();
            sum = sum + a[i];
        }
        System.out.println(sum);
    }
}

Comments