Java Program to Print Odd Elements at Even Index

«
»

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

Problem Description

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

advertisement

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

Output = 5

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

Program/Source Code
advertisement
advertisement

Here is the source code of the Java Program to Print Odd 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 Odd Elements at Even Index Number
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class EvenIndexedOddElements {
  8.     // Function to odd 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 whether the current index is even and the element at that index is odd.
3. The element is printed if the above mentioned condition is true.

advertisement

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

Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the size of the array
6
Enter array elements
2
1
4
4
5
7
Output
5
 
Case 2 (Simple Test Case - another example):
 
Enter the size of the array
8
Enter array elements
12
34
45
32
76
55
567
346
Output
45 567

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

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 & technical discussions at Telegram SanfoundryClasses.