Java Program to Print Even Elements at Even Index

This is the Java Program to Print Even Elements at Even Index Number.

Problem Description

Given an array of integers, print the even numbers present at even index numbers.
Example:

Array = [2,1,4,4,5,7]

Output = 2 4

Problem Solution

Iterate through the array, and check whether the current index is even or odd if it is even, then check the element at that index to be even or odd, print the element if it is even.

Program/Source Code
advertisement
advertisement

Here is the source code of the Java Program to Print Even Elements at Even Index Number. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Print Even Elements at Even Index Number
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class EvenIndexedOddElements {
  8.     // Function to even numbers present at even index
  9.     static void printEvenIndexedElements(int[] array){
  10.         int i=0;
  11.         for(i=0; i<array.length; i++){
  12.             if(i%2 == 0 && array[i]%2 ==0){
  13.                 System.out.print(array[i] + " ");
  14.             }
  15.         }
  16.     }
  17.     // Function to read input
  18.     public static void main(String[] args) {
  19.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  20.         int size;
  21.         System.out.println("Enter the size of the array");
  22.         try {
  23.             size = Integer.parseInt(br.readLine());
  24.         } catch (Exception e) {
  25.             System.out.println("Invalid Input");
  26.             return;
  27.         }
  28.         int[] array = new int[size];
  29.         System.out.println("Enter array elements");
  30.         int i;
  31.         for (i = 0; i < array.length; i++) {
  32.             try {
  33.                 array[i] = Integer.parseInt(br.readLine());
  34.             } catch (Exception e) {
  35.                 System.out.println("An error occurred");
  36.                 return;
  37.             }
  38.         }
  39.         System.out.println("Output");
  40.         printEvenIndexedElements(array);
  41.     }
  42. }
Program Explanation

1. In function printEvenIndexedElements(), the loop for(i=0; i<array.length; i++) is used to iterate through the array.
2. The condition if(i%2==0 && array[i]%2==0) checks if the index and the element at the index are both even.
3. The element is displayed if the condition is true.

Time Complexity: O(n) where n is the number of elements in the array.

Runtime Test Cases
 
Case 1 (Simple Test Case - Even elements at even index):
 
Enter the size of the array
6
Enter array elements
2
1
4
4
5
7
Output
2 4
 
Case 2 (Simple Test Case - Odd elements at even index):
 
Enter the size of the array
5
Enter array elements
1
2
3
4
5
Output

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.