sum of array of number
To calculate the sum of an array of numbers in C, you can use a loop to iterate through each element of the array and add its value to a running total. Here's an example code snippet that demonstrates this:
#include <stdio.h>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int sum = 0;
int size = sizeof(numbers) / sizeof(numbers[0]);
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
printf("The sum is: %d\n", sum);
return 0;
}
In this example, we define an array called numbers
with 5 elements. We initialize the sum
variable to 0, and then use a for
loop to iterate through each element of the array. Inside the loop, we add the value of each element to the sum
variable. Finally, we print the value of the sum
variable.
When you run this code, it will output:
The sum is: 15
This means the sum of the numbers in the array is 15.