Find the Sum of Even Numbers in an Array in Java

In this article, you will learn how to find the Sum of even numbers in an Array using Java. Here, we first check whether an array element is an Even number or not by dividing it by 2. If the remainder is 0 then the number is an Even number. We will add all found even numbers in a loop. Let’s see a few examples below:

Algorithm

  1. Declare and Initialize an integer array.
  2. Declare an integer variable evenSum = 0 to calculate the sum.
  3. Iterate the array using for loop from 0 to array length -1.
  4. Inside the loop, divide the array element by 2 and check whether the remainder is equal to 0 or not. eg: arr[i] % 2 == 0
  5. If the remainder is 0 then calculate the sum. eg: evenSum = evenSum + arr[i];
  6. Finally, print the evenSum.

Let’s jump to some examples now,

Example#1. Find the Sum of Even numbers in an Array

package com.javacodepoint.array;

public class FindEvenSumInArray {

	// Main method
	public static void main(String[] args) {

		// Declare and Initialize an integer array
		int arr[] = { 2, 4, 3, 8, 11, 50, 7, 15, 5 };

		// To calculate sum of even numbers
		int evenSum = 0;

		System.out.println("The sum of all Even numbers:");

		// Iterate the array
		for (int i = 0; i < arr.length; i++) {

			// Check whether the No. is an Even
			// if reminder is 0, it is an Even number
			if (arr[i] % 2 == 0) {

				if (evenSum > 0) {
					System.out.print(" + " + arr[i]);
				} else {
					System.out.print(arr[i]);
				}

				// Calculate the even sum
				evenSum = evenSum + arr[i];
			}
		}

		// Print the final result
		System.out.println(" = " + evenSum);

	}

}

OUTPUT:

The sum of all Even numbers:
2 + 4 + 8 + 50 = 64

Example#2. Calculate the Sum of all Even numbers in an Array using a Scanner

In this example, we use java.lang.Scanner class which is used to read input given by users. let’s see the java program code below:

package com.javacodepoint.array;

import java.util.Scanner;

public class FindEvenSumInArray2 {

	// Main method
	public static void main(String[] args) {

		// Creating scanner object for reading user inputs
		Scanner sc = new Scanner(System.in);

		// Read the size of the Array
		System.out.println("Enter the size of the Array: ");
		int n = sc.nextInt();

		// Declare the integer Array of given size
		int arr[] = new int[n];

		// Reading the Array values
		System.out.println("Enter " + n + " element(s) of the Array: ");
		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}

		// To calculate sum of even numbers
		int evenSum = 0;

		System.out.println("The sum of all Even numbers:");

		// Iterate the array
		for (int i = 0; i < arr.length; i++) {

			// Check whether the No. is an Even
			// if reminder is 0, it is an Even number
			if (arr[i] % 2 == 0) {

				if (evenSum > 0) {
					System.out.print(" + " + arr[i]);
				} else {
					System.out.print(arr[i]);
				}

				// Calculate the even sum
				evenSum = evenSum + arr[i];
			}
		}

		// Print the final result
		System.out.println(" = " + evenSum);

	}

}

OUTPUT:

Enter the size of the Array:
5
Enter 5 element(s) of the Array:
1
2
3
4
5
The sum of all Even numbers:
2 + 4 = 6

See also:
Java Program to Calculate Average Using Array.
Java Program to Merge Two Arrays.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

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