sum of arrays
Sum of Arrays in C
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
printf("The sum of the array elements is: %d", sum);
return 0;
}
- Declaration and Initialization:
- An array of integers
arr
is declared and initialized with values {1, 2, 3, 4, 5}. A variable
sum
is initialized to 0 to store the sum of the array elements.Sum Calculation:
- A
for
loop is used to iterate through the array elements. The value of each element is added to the
sum
variable.Output:
- The sum of the array elements is printed using
printf
.