HackerRank C- 1D Arrays in C




In this challenge, you have to create an array of size n dynamically, input the elements of the array, sum them and print the sum of the elements in a new line.



  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. int main() {
  6. int n,sum=0;
  7. scanf("%d",&n);
  8. int *a;
  9. a=(int*)malloc(n*sizeof(int));
  10. for(int i=0;i<n;i++)
  11. {
  12. scanf("%d",(a+i));
  13. sum=sum+*(a+i);
  14. }
  15. printf("%d",sum);
  16. return 0;
  17. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube