Simple Array Sum in C programming Language | | C -language Hackerrank Solutions

Simple Array Sum in C programming Language | | C -language Hacker rank Solutions


Simple Array Sum in C programming Language

Question:


Given an array of integers, find the sum of its elements.
Input Format
The first line contains an integer, , denoting the size of the array. 
The second line contains  space-separated integers representing the array's elements.
Output Format
Print the sum of the array's elements as a single integer.
Sample Input
6
1 2 3 4 10 11
Sample Output
31

Solution:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int simpleArraySum(int n, int ar_size, int* ar) {
    // Complete this function
    int sum=0;
    int i;
    for(i=0;i<n;i++){
        sum=sum+ar[i];
    }
    return sum;
}
int main() {
    int n;
    scanf("%i", &n);
    int *ar = malloc(sizeof(int) * n);
    for(int ar_i = 0; ar_i < n; ar_i++){
       scanf("%i",&ar[ar_i]);
    }
    int result = simpleArraySum(n, n, ar);
    printf("%d\n", result);
    return 0;
}

Comments :

Post a Comment