76.5k views
2 votes
Write a java program to find/print the index of all even values in an array

1 Answer

1 vote

Final answer:

The Java program provided finds and prints the index of all even numbers in an array using a for loop and modulus operator to check for even values.

Step-by-step explanation:

The student is asking for a Java program that can find and print the index of all even values in an array. Here is a sample Java program that accomplishes this task:

public class EvenIndexFinder {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6}; // Example array
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 == 0) {
System.out.println("Index of even number: " + i);
}
}
}
}

This program initializes an array with integers and uses a for loop to iterate through it. If an element is even, detected by the condition array[i] % 2 == 0, it prints the index of that element.

User Abdul Basit Khan
by
7.9k points