In this HackerRank Bitwise Operators problem in C programming, we need to make a program in c programming in which we need to create an array of size n dynamically and read the values from stdin. Iterate the array calculating the sum of all elements. Print the sum and free the memory where the array is stored.


HackerRank 1D Arrays in C problem solution

HackerRank 1D Arrays in C problem solution

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

int main()
{
  
    int n, *arr, i, sum = 0;
    scanf("%d", &n);
    arr = (int*) malloc(n * sizeof(int));
    for(i = 0; i < n; i++) {
        scanf("%d", arr + i);
    }

    for(i = 0; i < n; i++) {
        sum += *(arr + i);
    }

    printf("%d\n", sum);
    free(arr);
    return 0;
}