Java Program to Print Odd Elements at Even IndexIn Java, arrays are commonly used to store collections of data. When working with arrays, you might encounter scenarios where you need to perform operations based on specific criteria, such as printing elements that are odd and are located at even indices. In this section, we will create a Java program that achieves this, providing a detailed explanation and complete code. Problem StatementGiven an array of integers, print the elements that are odd and are located at even indices. Approach
Steps
File Name: OddElementsAtEvenIndex.java Output: Odd elements at even indices: Element at index 2: 15 Element at index 4: 19 Element at index 6: 23 ExplanationAn array is initialised with sample integers in the Java program to output odd elements at even indices, and a for loop is used to iterate over the array. We verify whether the index even applies the criterion i % 2 == 0 for every element. If the index is even, we use the condition array[i] % 2!= 0 to determine if the element at that index is odd. We print the element if the two requirements are satisfied. ConclusionThis Java program demonstrates how to identify and print odd elements that are located at even indices in an array. By using simple loops and conditional statements, you can efficiently filter, and display elements based on specific criteria. This technique can be extended to more complex scenarios and is a fundamental skill in array manipulation in Java. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India