# Program to find sum of elements in a given array

• Difficulty Level : Easy
• Last Updated : 24 Nov, 2021

Given an array of integers, find sum of its elements.
Examples :

Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out our self-paced courses designed for students of grades I-XII

Start with topics like Python, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, because GeeksforGeeks School is now here!

```Input : arr[] = {1, 2, 3}
Output : 6
1 + 2 + 3 = 6

Input : arr[] = {15, 12, 13, 10}
Output : 50```

## C++

 `/* C++ Program to find sum of elements``in a given array */``#include ``using` `namespace` `std;` `// function to return sum of elements``// in an array of size n``int` `sum(``int` `arr[], ``int` `n)``{``    ``int` `sum = 0; ``// initialize sum` `    ``// Iterate through all elements``    ``// and add them to sum``    ``for` `(``int` `i = 0; i < n; i++)``    ``sum += arr[i];` `    ``return` `sum;``}` `// Driver code``int` `main()``{``    ``int` `arr[] = {12, 3, 4, 15};``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``cout << ``"Sum of given array is "` `<< sum(arr, n);``    ``return` `0;``}` `// This code is contributed by rathbhupendra`

## C

 `/* C Program to find sum of elements`` ``in a given array */``#include ` `// function to return sum of elements``// in an array of size n``int` `sum(``int` `arr[], ``int` `n)``{``    ``int` `sum = 0; ``// initialize sum` `    ``// Iterate through all elements``    ``// and add them to sum``    ``for` `(``int` `i = 0; i < n; i++)``    ``sum += arr[i];` `    ``return` `sum;``}` `int` `main()``{``    ``int` `arr[] = {12, 3, 4, 15};``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``printf``(``"Sum of given array is %d"``, sum(arr, n));``    ``return` `0;``}`

## Java

 `/* Java Program to find sum of elements in a given array  */``class` `Test``{``     ``static` `int` `arr[] = {``12``,``3``,``4``,``15``};``     ` `     ``// method for sum of elements in an array``     ``static` `int` `sum()``     ``{``         ``int` `sum = ``0``; ``// initialize sum``         ``int` `i;``      ` `         ``// Iterate through all elements and add them to sum``         ``for` `(i = ``0``; i < arr.length; i++)``            ``sum +=  arr[i];``      ` `         ``return` `sum;``     ``}``     ` `     ``// Driver method``     ``public` `static` `void` `main(String[] args)``     ``{``         ``System.out.println(``"Sum of given array is "` `+ sum());``        ``}`` ``}`

## Python3

 `# Python 3 code to find sum``# of elements in given array``def` `_sum(arr,n):``    ` `    ``# return sum using sum``    ``# inbuilt sum() function``    ``return``(``sum``(arr))` `# driver function``arr``=``[]``# input values to list``arr ``=` `[``12``, ``3``, ``4``, ``15``]` `# calculating length of array``n ``=` `len``(arr)` `ans ``=` `_sum(arr,n)` `# display sum``print` `(``'Sum of the array is '``,ans)` `# This code is contributed by Himanshu Ranjan`

## C#

 `// C# Program to find sum of elements in a``// given array``using` `System;` `class` `GFG {``    ` `    ``// method for sum of elements in an array``    ``static` `int` `sum(``int` `[]arr, ``int` `n)``    ``{``        ` `        ``int` `sum = 0; ``// initialize sum``        ` `        ``// Iterate through all elements and``        ``// add them to sum``        ``for` `(``int` `i = 0; i < n; i++)``            ``sum += arr[i];``        ` `        ``return` `sum;``    ``}``    ` `    ``// Driver method``    ``public` `static` `void` `Main()``    ``{``        ` `        ``int` `[]arr = {12,3,4,15};``        ``int` `n = arr.Length;``        ` `        ``Console.Write(``"Sum of given array is "``                               ``+ sum(arr, n));``    ``}` `}` `// This code is contributed by Sam007.`

## PHP

 ``

## Javascript

 ``

Output :

`Sum of given array is 34`

Time Complexity: O(n)

Auxiliary Space: O(1)

Another Method: Using STL
Calling inbuilt function for sum of elements of an array in STL.
accumulate(first, last, sum);
first, last : first and last elements of range
whose elements are to be added
sum :  initial value of the sum

This function returns the array sum.

## C++

 `/* C++ Program to find sum of elements``in a given array */``#include ``using` `namespace` `std;` `// Driver code``int` `main()``{``    ``int` `arr[] = {12, 3, 4, 15};``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``      ``// calling accumulate function, passing first, last element and``    ``// initial sum, which is 0 in this case.``    ``cout << ``"Sum of given array is "` `<< accumulate(arr, arr + n, 0);``    ``return` `0;``}` `// This code is contributed by pranoy_coder`

## Java

 `/* Java Program to find sum of elements``in a given array */``import` `java.util.*;` `class` `GFG{` `    ``static` `int` `accumulate(``int` `[]arr)``        ``{``            ``int` `sum = ``0``;``            ``for``(``int` `i = ``0``; i < arr.length; i++)``            ``{``                ``sum = sum + arr[i];``            ``}``            ``return` `sum;``        ``}``     ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = {``12``, ``3``, ``4``, ``15``};``    ``int` `n = arr.length;``  ` `      ``// calling accumulate function, passing first, last element and``    ``// initial sum, which is 0 in this case.``    ``System.out.print(``"Sum of given array is "` `+  accumulate(arr));``}``}` `// This code is contributed by umadevi9616`

## Python3

 `# Python3 program to find sum of elements``# in a given array` `# Driver code``if` `__name__ ``=``=` `"__main__"``:``  ` `    ``arr ``=` `[ ``12``, ``3``, ``4``, ``15` `]``    ``n ``=` `len``(arr)``    ` `    ``# Calling accumulate function, passing``    ``# first, last element and initial sum,``    ``# which is 0 in this case.``    ``print``(``"Sum of given array is "``, ``sum``(arr));` `# This code is contributed by ukasp`

## C#

 `/* C# Program to find sum of elements``in a given array */``using` `System;``public` `class` `GFG {` `    ``static` `int` `accumulate(``int``[] arr) {``        ``int` `sum = 0;``        ``for` `(``int` `i = 0; i < arr.Length; i++) {``            ``sum = sum + arr[i];``        ``}``        ``return` `sum;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `[]arr = { 12, 3, 4, 15 };``        ``int` `n = arr.Length;` `        ``// calling accumulate function, passing first, last element and``        ``// initial sum, which is 0 in this case.``        ``Console.Write(``"Sum of given array is "` `+ accumulate(arr));``    ``}``}` `// This code is contributed by gauravrajput1`

## Javascript

 ``
Output
`Sum of given array is 34`

My Personal Notes arrow_drop_up