Thursday, February 4, 2016

Simple Array Sum - Hacker Rank Solution

You are given an array of integers of size N. Can you find the sum of the elements in the array?
Input
The first line of input consists of an integer N. The next line contains N space-separated integers representing the array elements.

Sample:
6
1 2 3 4 10 11
Output
Output a single value equal to the sum of the elements in the array.
For the sample above you would just print 31 since 1+2+3+4+10+11=31 .

------------------------------------------------------------------------------------------------------

  Simple Array  Sum  - Hacker Rank Solution  

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int a[1000],n=1,i=0,result=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        {
        scanf("%d ",&a[i]);
        result=result+a[i];
        }
    printf("%d",result);
      
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}

  Simple Array  Sum  - Hacker Rank Solution 

 -----------------------------------------------------------------------------------------------------------

 

1+2+3+4+10+11=31.
Powered by Blogger.