In this tutorial, we are going to solve HackerRank 1D Arrays in c problem solution with practical program code example and step-by-step explanation. in this problem, we need to take an integer input that will give the number of integer values we need to read from the next line and in the next line we have a number of integer values separated by space. after that, we need to print the sum of all the integers in the array.
using this problem we are going to learn about the dynamic arrays in the c programming and how we can read the values dynamically and then iterate the array to calculate the sum of all the elements. there are the number of ways to solve this problem.
![1D array in C problem solution](https://programmingoneonone.com/wp-content/uploads/2024/07/HackerRank-1D-array-in-c-problem-solution.webp)
Solution in C programming
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n,sum=0;
scanf("%d",&n);
int *val = malloc(n*sizeof(int));
for(int i=0;i<n;i++)
{
scanf("%d",&val[i]);
sum+=val[i];
}
printf("%i",sum);
free(val);
}
Second solution
#include <stdio.h>
int main()
{
int n,val,sum=0;
scanf("%d",&n);
while(n--)
{
scanf("%d",&val);
sum+=val;
}
printf("%i",sum);
}