 Open in App
Not now

# C/C++ Program to find sum of elements in a given array

• Difficulty Level : Basic
• Last Updated : 13 Jun, 2022

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

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

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

## CPP

 `/* CPP 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;``}`

Time complexity: O(n)

Auxiliary Space: O(1)

Please refer complete article on Program to find sum of elements in a given array for more details!

My Personal Notes arrow_drop_up