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

## 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()); ` ` ` `} ` ` ` `} ` |

*chevron_right*

*filter_none*

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

## Recommended Posts:

- Java Program for Program to find area of a circle
- Java Program for Program for array rotation
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Java Program to Find sum of even factors of a number
- Java Program to Find minimum sum of factors of number
- Java Program to Find sum of Series with n-th term as n^2 - (n-1)^2
- Java Program to Find remainder of array multiplication divided by n
- Java Program to find largest element in an array
- Java Program to Find Maximum Odd Number in Array Using Stream and Filter
- Java Program for Program to calculate area of a Tetrahedron
- Program for Find sum of odd factors of a number
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Java Program for Range sum queries without updates
- Java Program for cube sum of first n natural numbers
- Java Program for Sum of squares of first n natural numbers
- Java Program for Sum the digits of a given number
- Java Program for Subset Sum Problem | DP-25
- Java Program for Maximum sum rectangle in a 2D matrix | DP-27
- Java Program for Largest Sum Contiguous Subarray
- Java Program for Find the perimeter of a cylinder