Java program 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 those 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 even numbers.

Below are the steps or algorithm that we will use:

Algorithm to use:

  • Take the total count of numbers from the user.
  • Create one integer array equal to the value of user-given count.
  • Ask the user to enter the numbers for the array one by one. Read the numbers and insert it in the integer array.
  • Initialize one sum variable to hold the total sum of all even numbers. Initialize it as 0.
  • Run one loop and check for each digit of the array. For each digit, check if it is an even number or not. If it is an even number, add it to the value of the sum variable. So, for each even number we will get, the value of sum will increase. At the end, sum will hold the total sum of even numbers in the array.
  • Once the loop ends, sum will hold the total sum of even numbers in the array. Print its value.

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

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 count.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        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);
    }
}

Here,

  • It asks the user to enter the size of the array and this value is stored in size variable.
  • intArr is an integer array of size equal to the size that the user entered.
  • Using a for loop, it reads the elements of the array from the user.
  • sum variable is initialized as 0. This is to hold the sum of all even numbers in the array.
  • Using another for loop, we are iterating through the integer array intArr and checking if the current number is even or not. If it is, we are adding it to sum.
  • Finally, it prints the calculated sum i.e. sum of all even numbers in the array.

It will give output as like 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 like below:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        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);
    }
}

It will print output similar to the above program.

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 main:

import java.util.Scanner;

public class Main {

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

Here,

  • findEvenSum method is used to find the sum of all even numbers in the array.
  • It does the same thing. We are using a while loop but you can also use a for loop.

It will print a similar output.

You might also like: