This program calculates the sum of all elements in an array.
#include <stdio.h>
int
main() {
int n;
printf(
"Enter the number of elements: ");
scanf(
"%d", &n);
int arr[n], sum =
0;
printf(
"Enter %d elements: ", n);
for (
int i =
0; i < n; ++i) {
scanf(
"%d", &arr[i]);
sum += arr[i];
}
printf(
"Sum of elements: %d\n", sum);
return
0;
}
No comments:
Post a Comment