1console.log(
2 [1, 2, 3, 4].reduce((a, b) => a + b, 0)
3)
4console.log(
5 [].reduce((a, b) => a + b, 0)
6)
1var numbers = [10, 20, 30, 40]
2var sum = 0;
3for (var i = 0; i < numbers.length; i++) { sum += numbers[i]}
1/**
2 * C program to find sum of all elements of array
3 */
4
5#include <stdio.h>
6#define MAX_SIZE 100
7
8int main()
9{
10 int arr[MAX_SIZE];
11 int i, n, sum=0;
12
13 /* Input size of the array */
14 printf("Enter size of the array: ");
15 scanf("%d", &n);
16
17 /* Input elements in array */
18 printf("Enter %d elements in the array: ", n);
19 for(i=0; i<n; i++)
20 {
21 scanf("%d", &arr[i]);
22
23 // Add each array element to sum
24 sum += arr[i];
25 }
26
27 printf("Sum of all elements of array = %d", sum);
28
29 return 0;
30}
1const sum = (...args) => args.reduce((a, b) => a + b, 0);
2console.log(sum(1,2,3)); // returns 6