Java Program to Print Odd Elements at Odd Index

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

Problem Description

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

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

Output = 1,7

Problem Solution

Iterate through the array, and check whether the current index is even or odd. If it is odd 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 Odd 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 Odd Index Number
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class OddIndexedOddElements {
  8.     // Function to print Odd Elements present at Odd Index Number
  9.     static void printOddIndexedElements(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.  
  18.     // Function to read user input
  19.     public static void main(String[] args) {
  20.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  21.         int size;
  22.         System.out.println("Enter the size of the array");
  23.         try {
  24.             size = Integer.parseInt(br.readLine());
  25.         } catch (Exception e) {
  26.             System.out.println("Invalid Input");
  27.             return;
  28.         }
  29.         int[] array = new int[size];
  30.         System.out.println("Enter array elements");
  31.         int i;
  32.         for (i = 0; i < array.length; i++) {
  33.             try {
  34.                 array[i] = Integer.parseInt(br.readLine());
  35.             } catch (Exception e) {
  36.                 System.out.println("An error occurred");
  37.                 return;
  38.             }
  39.         }
  40.         System.out.println("Output");
  41.         printOddIndexedElements(array);
  42.     }
  43. }
Program Explanation

1. In function printOddIndexedElements(), the loop for(i=0; i<array.length; i++) iterates through the array.
2. Then using the condition if(i%2 != 0 && array[i]%2 != 0), we first check whether the current index is odd.
3. If the index is odd, then we check the element at that index (the condition after the && operator), if the element is odd, then display the element.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

Runtime Test Cases
 
Case 1 (Positive test Case - having odd elements at odd index):
 
Enter the size of the array
6
Enter array elements
2
1
4
4
5
7
Output
1 7 
 
Case 2 (Negative test Case - no odd element present at odd 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.