Sum of Array in Java

Sum of Array in Java | Sum of Array Elements in Java | Array Addition in Java | Program Description:- Write a Java program to find the sum of array elements in Java.

Examples of sum of array elements. Assume we have a given array,

array[] = {10, 20, 30}
Sum of array elements = 10+20+30 = 60

Similarly,
array[] = {50, 60, -20, 55, -90}
Sum of array elements = 50+60-20+55-90 = 55

Procedure to find the sum of array elements,
1) Take one array.
2) Declare one sum variable and initialize it with 0.
3) Using a loop, Iterate through the elements of the given array.
4) Add element to the sum variable and assign result value back to the sum variable. (i.e. sum = sum + arr[i])
5) When all elements of the array are added to the sum variable then display the result.

Now, let us see the array addition in Java and display the result. First, we will develop a program by hardcoding the values, and again we develop another Java program to take input from the end-user and then find the sum of array elements.

Java program to calculate the sum of array elements

public class ArraySum {
public static void main(String[] args) {

// declare array and
// initialize it with values
int array[] = {10, 20, 30, 40, 50};

// declare sum variable and
// initialize it with 0
int sum = 0;

// loop to iterate through the array
for (int i=0; i<array.length; i++) {
// add element to the sum
sum += array[i];
// or,
// sum = sum + array[i];
}

// display the result
System.out.println("Sum of array elements= " + sum);

}
}

Output:-

Sum of array elements= 150

In the previous program we have hardcoded the values, now let us develop another Java program to solve the same problem but take input values from the end-user.

Java program to calculate the sum of array elements by taking input values

import java.util.Scanner;
public class ArraySum {
public static void main(String[] args) {

// create Scanner class object
Scanner scan = new Scanner(System.in);

// take number of elements in the array
System.out.print("Enter number of elements: ");
int number = scan.nextInt();

// declare array with given size
int array[] = new int[number];

// take input for array elements
System.out.println("Enter array elements: ");
for (int i=0; i<array.length; i++) {
array[i] = scan.nextInt();
}

// declare sum variable and
// initialize it with 0
int sum = 0;

// loop to iterate through the array
for (int i=0; i<array.length; i++) {
// add array elements to sum
sum += array[i];
}

// display the result
System.out.println("Sum of array elements= " + sum);

}
}

Output for the different test-cases:-

Enter number of elements: 5
Enter array elements:
10 20 30 40 50
Sum of array elements= 150

Enter number of elements: 3
Enter array elements:
150 -100 120
Sum of array elements= 170

In the above program, instead of array.length you can also use numbers, both hold the same value. Instead of Scanner class you can also use BufferReader class. To iterate through the loop we can also use for-each loop rather than simple for loop.

// for-each loop
for (int i : list) {
// add array elements to sum
sum += i;
}

The limitation of the above program is:- the number of array elements must be known. If the number of array elements is dynamic and can’t be predicted before then we must take the help of collection classes becauase they can grow dynamically.

import java.util.List;
import java.util.ArrayList;
public class ArraySum {
public static void main(String[] args) {

// declare list
List<Integer> list = new ArrayList<Integer>();

// add elements to the list
// you can take input from the end-user

// declare sum variable and
// initialize it with 0
int sum = 0;

// for-each loop to iterate
for (int i : list) {
// add array elements to sum
sum += i;
}

// display the result
System.out.println("Sum = " + sum);

}
}

Output:-

Sum = 150

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!