Answers for "sum elements in array"

2

sum an array in javascript

const arr = [1,2,3,4,5]
const sum = arr.reduce((a,b) => a+b)
Posted by: Guest on October-03-2021
8

js sum of array

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

// Output: 10
Posted by: Guest on July-26-2020
12

javascript sum of array

const sum = arr => arr.reduce((a, b) => a + b, 0);
Posted by: Guest on July-03-2020
0

how to find the sum of array using JavaScript

// If you do not want to use array.reduece you can itereate through the array and then get the solution.
const  getSum = (arr)=>{
  let sum = 0;
  for(key of arr){
    sum += key
  }
  return sum
}
Posted by: Guest on October-03-2021
1

sum of arrays

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

#include <stdio.h>
#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<n; i++)
    {
        scanf("%d", &arr[i]);

        // Add each array element to sum
        sum += arr[i];
    }

    printf("Sum of all elements of array = %d", sum);

    return 0;
}
Posted by: Guest on April-29-2020
0

sum of arrays

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

#include <stdio.h>
#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<n; i++)
    {
        scanf("%d", &arr[i]);
    }

    /*
     * Add each array element to sum
     */
    for(i=0; i<n; i++)
    {
        sum = sum + arr[i];
    }


    printf("Sum of all elements of array = %d", sum);

    return 0;
}
Posted by: Guest on April-29-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language