Site icon Matistics

C Program to Calculate the Sum of All Elements in an Array. The program will prompt the user to input the array size and the individual array elements.

#include<stdio.h>
int main(){
int n,sum=0;
printf("Enter the length of array ");
scanf("%d",&n);
int a[n];
printf("Enter the elements of array\n");
for(int i=0;i<n;i++){
scanf("%d",&a[i]);\
}
for(int j=0;j<n;j++){
sum += a[j];
}
printf("Sum: %d",sum);
return 0;
}
Exit mobile version