Given an array of integers. The task is to calculate the count of a number of elements which are divisible by a given number k.

**Examples:**

Input:arr[] = { 2, 6, 7, 12, 14, 18 }, k = 3Output:3 Numbers which are divisible by k are { 6, 12, 18 }Input:arr[] = { 2, 6, 7, 12, 14, 18 }, k = 2Output:5

**Method-1:** Start traversing the array and check if the current element is divisible by K. If yes then increment the count. Print the count when all the elements get traversed.

Below is the implementation of the above approach:

## C++

`// C++ program to Count the number of elements ` `// in an array which are divisible by k ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to count the elements ` `int` `CountTheElements(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `counter = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(arr[i] % k == 0) ` ` ` `counter++; ` ` ` `} ` ` ` ` ` `return` `counter; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 6, 7, 12, 14, 18 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 3; ` ` ` ` ` `cout << CountTheElements(arr, n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to Count the number of elements ` `// in an array which are divisible by k ` `import` `java.util.*; ` ` ` `class` `Geeks { ` ` ` ` ` `// Function to count the elements ` `static` `int` `CountTheElements(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `counter = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `if` `(arr[i] % k == ` `0` `) ` ` ` `counter++; ` ` ` `} ` ` ` ` ` `return` `counter; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `arr[] = { ` `2` `, ` `6` `, ` `7` `, ` `12` `, ` `14` `, ` `18` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `k = ` `3` `; ` ` ` ` ` `System.out.println(CountTheElements(arr, n, k)); ` `} ` `} ` ` ` `// This code is contributed by ankita_saini ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to Count the ` `# number of elements in an array ` `# which are divisible by k ` ` ` `# Function to count the elements ` `def` `CountTheElements(arr, n, k): ` ` ` `counter ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, n, ` `1` `): ` ` ` `if` `(arr[i] ` `%` `k ` `=` `=` `0` `): ` ` ` `counter ` `=` `counter` `+` `1` ` ` ` ` `return` `counter ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `2` `, ` `6` `, ` `7` `, ` `12` `, ` `14` `, ` `18` `]; ` ` ` `n ` `=` `len` `(arr) ` ` ` `k ` `=` `3` ` ` ` ` `print` `(CountTheElements(arr, n, k)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to Count the number of elements ` `// in an array which are divisible by k ` `using` `System; ` ` ` `class` `Geeks { ` ` ` ` ` `// Function to count the elements ` `static` `int` `CountTheElements(` `int` `[]arr, ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `counter = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(arr[i] % k == 0) ` ` ` `counter++; ` ` ` `} ` ` ` ` ` `return` `counter; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]arr = { 2, 6, 7, 12, 14, 18 }; ` ` ` `int` `n = arr.Length; ` ` ` `int` `k = 3; ` ` ` ` ` `Console.WriteLine(CountTheElements(arr, n, k)); ` `} ` `} ` `//This code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to Count the number of elements ` `// in an array which are divisible by k ` ` ` `// Function to count the elements ` `function` `CountTheElements(` `$arr` `, ` `$n` `, ` `$k` `) ` `{ ` ` ` `$counter` `= 0; ` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `if` `(` `$arr` `[` `$i` `] % ` `$k` `== 0) ` ` ` `$counter` `++; ` ` ` `} ` ` ` ` ` `return` `$counter` `; ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `( 2, 6, 7, 12, 14, 18 ); ` `$n` `= ` `count` `(` `$arr` `); ` `$k` `= 3; ` ` ` `echo` `CountTheElements(` `$arr` `, ` `$n` `, ` `$k` `); ` ` ` `// This code is contributed by inder_verma ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

**Method-2:** Another way to do that is to use inbuilt function – Count_if . This functions takes in pointers to beginning and ending of the container containing the elements and auxiliary function. It will return the count of elements for which the function will return true.

Below is the implementation of the above approach:

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `int` `main() ` `{ ` ` ` `vector<` `int` `> v{ 2, 6, 7, 12, 14, 18 }; ` ` ` ` ` `// Count the number elements which when passed ` ` ` `// through the function (3rd argument) returns true ` ` ` `int` `res = count_if(v.begin(), v.end(), ` ` ` `[](` `int` `i, ` `int` `k = 3) { ` `return` `i % k == 0; }); ` ` ` `cout << res; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N)

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:

- Count of pairs of Array elements which are divisible by K when concatenated
- Count of elements on the left which are divisible by current element
- Count of elements on the left which are divisible by current element | Set 2
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Count of elements not divisible by any other elements of Array
- Count all prefixes of the given binary array which are divisible by x
- Count of array elements which is smaller than both its adjacent elements
- Count of array elements which are greater than all elements on its left
- Minimum and Maximum element of an array which is divisible by a given number k
- Count the number of elements which are greater than any of element on right side of an array
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Maximum possible elements which are divisible by 2
- Count of numbers in a range that does not contain the digit M and which is divisible by M.
- Count numbers in a range that are divisible by all array elements
- Count of elements which are equal to the XOR of the next two elements
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Sum and Product of all Composite numbers which are divisible by k in an array
- Find the element before which all the elements are smaller than it, and after which all are greater
- Maximum and Minimum element of a linked list which is divisible by a given number k

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.