Given an array, the task is to find elements which are odd and even, using STL in C++

**Examples:**

Input:a[] = {1, 2, 3, 4, 5, 10}Output:Odd = 3, Even = 3Input:a[] = {4, 3, 5, 9, 11}Output:Odd = 4, Even = 1

**Approach:** This can be achieved using count_if() method in C++

**Syntax:**

count_if(lower_bound, upper_bound, function)

where, **function** takes the element of given sequence one by one as a parameter and returns a boolean value on the basis of condition specified in that function.

In this case, the function will be:

bool isOdd(int i) { if (i % 2 != 0) return true; else return false; }

Below is the implementation of the above approach:

`// C++ simple program to ` `// find elements which are ` `// odd and even ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check ` `// if the element is off or even ` `bool` `isOdd(` `int` `i) ` `{ ` ` ` `if` `(i % 2 != 0) ` ` ` `return` `true` `; ` ` ` `else` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 2, 6, 3, 4, 5 }; ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `int` `count = count_if(a, a + n, isOdd); ` ` ` ` ` `cout << ` `"Odd: "` `<< count << endl; ` ` ` `cout << ` `"Even: "` `<< (n - count) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Odd: 3 Even: 3

Rated as one of the most sought after skills in the industry, own the basics of coding with our **C++ STL** Course and master the very concepts by intense problem-solving.

## Recommended Posts:

- Find elements of an array which are divisible by N using STL in C++
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Number of permutations such that sum of elements at odd index and even index are equal
- Generate an array of given size with equal count and sum of odd and even numbers
- How to find the sum of elements of an Array using STL in C++?
- Even digits Sum and Odd digits sum divisible by 4 and 3 respectively
- Print "Even" or "Odd" without using conditional statement
- Odd Even Transposition Sort / Brick Sort using pthreads
- Elements present in first array and not in second using STL in C++
- How to find common elements between two Arrays using STL in C++?
- How to find common elements between two Vector using STL in C++?
- How to find the sum of elements of a Vector using STL in C++?
- How to find the minimum and maximum element of an Array using STL in C++?
- C++ program to print all Even and Odd numbers from 1 to N
- Check whether count of odd and even factors of a number are equal
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Remove duplicate elements in an Array using STL in C++
- Check all the elements in an array are even using library in C++
- How to find the maximum element of an Array using STL in C++?

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.