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 the Sum of First N Odd & Even Numbers
- Java Program to Reverse a Number and find the Sum of its Digits Using do-while Loop
- Java Program to Find Sum of N Numbers Using Recursion
- Java Program to Find Sum of Natural Numbers Using While Loop
- 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 to Find 2 Elements in the Array such that Difference Between them is Largest
- Java Program to Check Array Bounds while Inputing Elements into the Array
- 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