Tuesday, 29 September 2015


Problem Statement
You are given an array of integers of size N. You need to print the sum of the elements of the array.
Note: The range of the 32-bit integer is 231 to 2311 or [2147483648,2147483647]. When we add several integer values, the resulting sum might exceed this range. You might need to use long long int in C/C++ or long data type in Java to store such sums.
Input Format
The first line of the input consists of an integer N. The next line contains N space-separated integers describing the array.
Constraints
1N10
0A[i]1010
Output Format
Output a single value equal to the sum of the elements of the array.
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005
Sample Output
5000000015

Solution:-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int N;
    int i;
    long sum=0;
    cin>>N;
      int num[N];
    if(N>10)
        return 0;
    for(i=0;i<N;i++)
        {
        cin>>num[i];
    }
    for(i=0;i<N;i++)
        {
        sum=sum+num[i];
    }
    cout<<sum;
    return 0;
}

No comments:

Post a Comment