Related Articles
Find elements of an Array which are Odd and Even using STL in C++
• Last Updated : 18 Dec, 2019

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 = 3

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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); ` ` `  `    ``int` `count = count_if(a, a + n, isOdd); ` ` `  `    ``cout << ``"Odd: "` `<< count << endl; ` `    ``cout << ``"Even: "` `<< (n - count) << endl; ` ` `  `    ``return` `0; ` `} `

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :