# Program to find sum of elements in a given array

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
```

## C

```/* C Program to find sum of elements in a given array */
#include <stdio.h>

// C program to return sum of elements in an array of size n
int sum(int arr[], int n)
{
int sum = 0; // initialize sum
int i;

// Iterate through all elements and add them to sum
for (i = 0; i < n; i++)
sum += arr[i];

return sum;
}

int main()
{
int arr[] = {12,3,4,15};
int n = sizeof(arr)/sizeof(arr[0]);
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());
}
}
```

Output:
`Sum of given array is 34`

# GATE CS Corner    Company Wise Coding Practice

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.