C++ Code Example: odd numbers in an array

This is a C++ program that prints the odd elements in an integer array.

  1. The program starts by defining an integer array numbers with 9 elements.
  2. Then it calculates the length of the array by subtracting the begin iterator from the end iterator of the numbers array, and assigns it to the arrayLength variable.
  3. In the for loop, it iterates over the numbers array from index 0 to arrayLength - 1 inclusive.
  4. For each iteration, it checks if the current element in the array (numbers[i]) is odd or not, by checking if its remainder when divided by 2 is not equal to 0 (numbers[i] % 2 != 0).
  5. If the element is odd, the program outputs it followed by a space character.
#include <iostream>
using namespace std;

int main() {
    int numbers[] = {33, 431, 41, 76, 83, 99, 46, 9, 22};
    int arrayLength = end(numbers) - begin(numbers);

    for (int i = 0; i < arrayLength; i++) {
        if (numbers[i] % 2 != 0) {
            cout << numbers[i] << " ";
        }
    }

    return 0;
}
Output
33 431 41 83 99 9