Given an array and an integer N, find elements which are divisible by N, using STL in C++

**Examples:**

Input:a[] = {1, 2, 3, 4, 5, 10}, N = 2Output:3Explanation:As 2, 4, and 10 are divisible by 2 Therefore the output is 3Input:a[] = {4, 3, 5, 9, 11}, N = 5Output:1Explanation:As only 5 is divisible by 5 Therefore the output is 3

**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 isDiv(int i) { if (i % N == 0) return true; else return false; }

Below is the implementation of the above approach:

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

*chevron_right*

*filter_none*

**Output:**

Elements divisible by 2: 3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find elements of an Array which are Odd and Even using STL in C++
- How to find the sum of elements of an Array 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++?
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Elements present in first array and not in second using STL in C++
- Remove duplicate elements in an Array using STL in C++
- Sum of first N natural numbers which are divisible by X or Y
- How to find the maximum element of an Array using STL in C++?
- How to find the minimum and maximum element of an Array using STL in C++?
- Sorting an array according to another array using pair in STL
- Count smaller elements on right side using Set in C++ STL
- Find and print duplicate words in std::vector<string> using STL functions
- How to find the maximum element of a Vector using STL in C++?
- How to find the minimum and maximum element of a Vector using STL in C++?
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Count number of unique Triangles using STL | Set 1 (Using set)
- Count the number of 1's and 0's in a binary array using STL in C++ ?
- Array product in C++ using STL

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.