Java Code Example: odd numbers in an array

This Java program prints out all the odd numbers in an array. The array of numbers is hardcoded into the program and it contains 9 elements.

A for loop is used to iterate over each element in the array. For each iteration, the code checks if the current element is an odd number by using the modulus operator %. If the result of numbers[i] % 2 is not equal to 0, it means that the current element is an odd number and it gets printed to the console. The loop continues until all the elements of the array have been processed.

import java.util.Scanner;

public class OddNumbersInArray {
    public static void main(String[] args) {
        int numbers[] = { 65, 34, 23, 75, 76, 33, 12, 9, 44 };

        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] % 2 != 0) {
                System.out.print(numbers[i] + " ");
            }
        }
    }
}
Output
65 23 75 33 9