

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find elements of an array which are divisible by N using STL in C++
Given with an array and the task is to find the number which are divisible by N using standard template library in C++.
To solve this problem we are using the function count_if() present in C++ standard template library.
What is a count_if() function?
Syntax
count_if(LowerBound, UpperBound, function)
Description − This function returns the number of elements in an array that satisfies the given condition. It takes three parameters.
- Lower Bound − It points to the first element of an array or any other sequence.
- Upper Bound − It points to the last element of an array or any other sequence.
- Function − It returns the Boolean value on the basis of the condition specified.
Example
Input-: array[] = {2, 4, 1, 5, 8, 9} N = 4 Output-: Elements divisible by 4: 2 Input-: array[] = {1, 2, 3, 4, 5, 10} N = 2 Output: Elements divisible by 2: 3
Approach used in the below program is as follows −
- Input the integer values in an array of integer type.
- Create the bool function to check whether the element of an array is divisible by the user input value N.
- Call the function count_if() which takes the first and the last element and the function as the parameter.
Example
Live Demo
#include <bits/stdc++.h> using namespace std; int n; //function to check if the element is divisible by n bool check(int i) { if (i % n == 0) return true; else return false; } int main() { int arr[] = {2, 4, 1, 5, 8, 9}; n = 4; int size = sizeof(arr) / sizeof(arr[0]); int temp = count_if(arr, arr + size, check); cout<<"Elements divisible by "<<n<< ": " <<temp; return 0; }
Output
If we run the above code it will generate the following output −
Elements divisible by 4: 2
- Related Questions & Answers
- Find elements of an Array which are Odd and Even using STL in C++
- Count the number of elements in an array which are divisible by k in C++
- Find an array element such that all elements are divisible by it using c++
- Find permutation of n which is divisible by 3 but not divisible by 6 in C++
- Find N digits number which is divisible by D in C++
- Sum which is divisible by n in JavaScript
- Elements of an array that are not divisible by any element of another array in C++
- Sum of first N natural numbers which are divisible by X or Y
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N in C++
- How to find the sum of elements of an Array using STL in C++?
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- Count of m digit integers that are divisible by an integer n in C++
- Count all prefixes of the given binary array which are divisible by x in C++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Print array elements that are divisible by at-least one other in C++
Advertisements