Sum of Array in Java

Sum of Array in Java | Array Programs in Java – 5 | In the previous Java program, we have seen the different ways to print an array in Java. In this post, we will write a program to find the sum of an 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 the 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 array input from the end-user and then find the sum of array elements. To iterate through the array elements, you can use the length property of the array.

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};

    // initialize sum variable with 0
    int sum = 0;

    // add array elements
    for (int i=0; i<array.length; i++) {
      sum += array[i]; // 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 size of 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();
    }

    // initialize sum variable with 0
    int sum = 0;

    // add array elements to sum
    for (int i=0; i<array.length; i++) {
      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 the array.length you can also use numbers variable because both hold the same value. For simplicity, we are using the Scanner class to take input you can also use BufferReader class. To iterate through the loop we can also use a for-each loop rather than a simple for loop.

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

Limitation of the above program:- the number of array elements must be known at the program development time. If the number of array elements is dynamic and can’t be predicted before then we must take the help of collection classes because 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
    list.add(10);
    list.add(20);
    list.add(30);
    list.add(40);
    list.add(50);

    // initialize sum variable with 0
    int sum = 0;

    // add array elements to sum
    for (int i : list) {
      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 do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *