4 ways in Java to get the sum of even numbers in an Array

Java program to get the sum of even numbers in an array:

Even numbers are numbers that are completely divisible by 2. In this post, we will learn how we can find out the sum of all even numbers in an array in Java.

We will write one Java program that will take the numbers as inputs from the user, insert them in an integer array, and finally print out the total sum of all even numbers of the array.

Below are the steps of the algorithm that we will use:

Algorithm to use:

  • Take the total count of numbers as input from the user.
  • Create one integer array of size equal to the total count as entered by the user.
  • Ask the user to enter the numbers for the array one by one. Read the numbers and add them to the integer array.
  • Initialize one sum variable to hold the total sum of all even numbers. Initialize this variable as 0.
  • Run one loop to iterate over the numbers of the array. For each number, check if it is an even number or not. If it is an even number, add it to the value of the sum variable. Once the loop ends, the sum variable will hold the total sum of all the even numbers of the array.
  • Print the value of the sum variable at the end of the program.

We can use a for loop or while loop to solve this problem. In this post, I will show you examples of both for and while loops.

Example 1: Find the total sum of even numbers in an array using a for loop in Java:

In this example, we will use a for loop to find the total sum.

import java.util.Scanner;

public class ExampleOne {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("Enter the size of the array:");
            int size = sc.nextInt();

            int[] intArr = new int[size];

            System.out.println("Enter the elements of the array separated by space:");
            for (int i = 0; i < size; i++) {
                intArr[i] = sc.nextInt();
            }

            int sum = 0;
            for (int i = 0; i < size; i++) {
                if (intArr[i] % 2 == 0) {
                    sum += intArr[i];
                }
            }

            System.out.println("Sum of all even numbers in the array is: " + sum);
        }
    }
}

Download it on GitHub

Here,

  • It asks the user to enter the size of the array and this value is assigned to the variable size. It uses the Scanner object sc to read the user inputs.
  • The intArr is an integer array of size equal to the size variable, i.e. equal to the user input size.
  • Using a for loop, it reads the elements of the array from the user.
  • The sum variable is initialized as 0. This variable will hold the sum of all even numbers of the array.
  • Using another for loop, we are iterating over the elements of the array intArr. On each iteration, it checks if the current number is even or not. If it is an even number, it adds the value to the sum variable.
  • Finally, it prints the calculated sum i.e. the sum of all even numbers in the array.

It will give outputs as below:

Enter the size of the array: 
5
Enter the elements of the array separated by space: 
1 2 3 4 5
Sum of all even numbers in the array is: 6

java sum even in array

Example 2: Finding the sum of all even numbers using a while loop:

We can also implement the same program using a while loop as shown below:

import java.util.Scanner;

public class ExampleTwo {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("Enter the size of the array:");
            int size = sc.nextInt();

            int[] intArr = new int[size];

            System.out.println("Enter the elements of the array separated by space:");
            for (int i = 0; i < size; i++) {
                intArr[i] = sc.nextInt();
            }

            int sum = 0;
            int i = 0;

            while (i < size) {
                if (intArr[i] % 2 == 0) {
                    sum += intArr[i];
                }
                i++;
            }

            System.out.println("Sum of all even numbers in the array is: " + sum);
        }
    }
}

Download it on GitHub

It will print output similar to the above program.

Enter the size of the array:
4
Enter the elements of the array separated by space:
1 2 3 4
Sum of all even numbers in the array is: 6

The difference between a for loop and while loop is that the while loop checks the condition first before it iterates over its body. In the above example, it checks if the current value of i is smaller than size or not. If yes, it moves to the body part. At the end of the loop, it increments the value of i by 1.

Method 3: Finding the sum of even numbers using a different method:

We can put the code that is used to find the sum of all even numbers in the array in a separate method and call that method from the main method:

import java.util.Scanner;

public class ExampleThree {

    public static int findEvenSum(int[] arr, int size) {
        int sum = 0;
        int i = 0;

        while (i < size) {
            if (arr[i] % 2 == 0) {
                sum += arr[i];
            }
            i++;
        }
        return sum;
    }

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("Enter the size of the array:");
            int size = sc.nextInt();

            int[] intArr = new int[size];

            System.out.println("Enter the elements of the array separated by space:");
            for (int i = 0; i < size; i++) {
                intArr[i] = sc.nextInt();
            }

            System.out.println("Sum of all even numbers in the array is: " + findEvenSum(intArr, size));
        }
    }
}

Download it on GitHub

Here,

  • The findEvenSum method is used to find the sum of all even numbers in the array.
  • It uses a while loop to find the sum. It returns that value to the main function.

It will print a similar output.

Example 4: By converting the array to a stream:

We can convert the array to a stream with the Arrays.stream method. The filter method can be used to filter out the even numbers and the sum method returns the sum of all filtered even values. The following program shows how we can find the sum by converting the array to a stream:

import java.util.Arrays;
import java.util.Scanner;

public class ExampleFour {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("Enter the size of the array:");
            int size = sc.nextInt();

            int[] intArr = new int[size];

            System.out.println("Enter the elements of the array separated by space:");
            for (int i = 0; i < size; i++) {
                intArr[i] = sc.nextInt();
            }

            int sum = Arrays.stream(intArr).filter(item -> item %2 == 0).sum();

            System.out.println("Sum of all even numbers in the array is: " + sum);
        }
    }
}

Download it on GitHub

You might also like: