sum of an array code example

Example 1: js sum of array

[1, 2, 3, 4].reduce((a, b) => a + b, 0)

// Output: 10

Example 2: javascript sum of array

const sum = arr => arr.reduce((a, b) => a + b, 0);

Example 3: how to add up all numbers in an array

var numbers = [10, 20, 30, 40] 
var sum = 0;
for (var i = 0; i < numbers.length; i++) {  sum += numbers[i]}

Example 4: sum of an array

const sum = (...args) => args.reduce((a, b) => a + b, 0);
console.log(sum(1,2,3)); // returns 6

Example 5: sum of arrays

/**
 * C program to find sum of all elements of array
 */

#include 
#define MAX_SIZE 100

int main()
{
    int arr[MAX_SIZE];
    int i, n, sum=0;

    /* Input size of the array */
    printf("Enter size of the array: ");
    scanf("%d", &n);

    /* Input elements in array */
    printf("Enter %d elements in the array: ", n);
    for(i=0; i

Example 6: sum of arrays

/**
 * C program to find sum of all elements of array 
 */

#include 
#define MAX_SIZE 100

int main()
{
    int arr[MAX_SIZE];
    int i, n, sum=0;

    /* Input size of the array */
    printf("Enter size of the array: ");
    scanf("%d", &n);

    /* Input elements in array */
    printf("Enter %d elements in the array: ", n);
    for(i=0; i

Tags:

Misc Example