Java Program to Find the Sum of Array Elements

In this tutorial, we will learn writing a java program to create an array and calculate the sum of elements stored in the array.

Our program will first take the input of array size and then the elements of the array. And then calculate the sum of elements of the input array and print as an output.

For Example

Case 1: if the user inputs 4 as array size and the array elements as 1,2,3,4.

             The output should be 10.

Case 2: if the user inputs 5 as array size and the array elements as 9,8,7,6,5.

             The output should be 35.

Java Program to Find the Sum of Array Elements

Output

Explanation

For the input array [1, 2, 3, 4, 5], value of sum = 1 + 2 + 3 + 4 + 5 = 15.