Java Program to Print All Even Numbers in Array

In this tutorial, we will learn writing Java program to create an array and print the even elements stored in the array. Our program will first take the input of array size and then the elements of the array. And then prints all the even numbers from the elements of the input array.

For Example

Case 1: If the user inputs 4 as array size and the array elements as 1,2,3,4.

           The output should be 2, 4.

Case 2: If the user inputs 5 as array size and the array elements as 9,8,7,6,5.

            The output should be 8, 6.

Java Program to Print All Even Numbers in Array

Output

Java Program to Print All Even Numbers in Array

Explanation

For the input array [1, 2, 3, 4, 5] , while iterating the elements of the array. For elements 2 and 4, the if-condition is satisfied and then the numbers 2 and 4 are printed as the output of the program.