Sunday, 16 August 2015

Simple Array Sum Hacker Rank Problem Solution Using C++.

/*
Problem Statement
You are given an array of integers of size N. You need to print the sum of the elements of the array.
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
1N1000
0A[i]1000
Output Format
Output a single value equal to the sum of the elements of the array.
Sample Input
6
1 2 3 4 10 11
Sample Output
31
Explanation
1+2+3+4+10+11=31
TAG: C++,Array Implementation , Simple Array Sum, Hacker Rank
*/

Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {

   int n,sum = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        int a;cin >> a;
        sum+=a;
    }
    cout << sum << endl;
    return 0;
}

3 comments:

  1. Hey dude ,u have to use validation for N (1≤N≤1000) , so N should not accept more than 1000,ur program will be failed in many test cases

    ReplyDelete
    Replies
    1. tnx.. but here N not greater than 1000.so, i don't need the validation.

      Delete
    2. This comment has been removed by the author.

      Delete