sum of elements in an arrau code example
Example 1: how to add up all numbers in an array
const numbers = [10, 20, 30, 40]
add = (a, b) => a + b
const sum = numbers.reduce(add)
Example 2: sum of arrays
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int i, n, sum=0;
printf("Enter size of the array: ");
scanf("%d", &n);
printf("Enter %d elements in the array: ", n);
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum of all elements of array = %d", sum);
return 0;
}