Given an array containing N elements and a number K. The task is to find the product of all such elements of the array which are divisible by K.

**Examples**:

Input : arr[] = {15, 16, 10, 9, 6, 7, 17} K = 3 Output : 810 Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9} K = 2 Output : 384

The idea is to traverse the array and check the elements one by one. If an element is divisible by K then multiply that element’s value with the product so far and continue this process while the end of the array is reached.

Below is the implementation of the above approach:

## C++

`// C++ program to find Product of all the elements ` `// in an array divisible by a given number K ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to find Product of all the elements ` `// in an array divisible by a given number K ` `int` `findProduct(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `prod = 1; ` ` ` ` ` `// Traverse the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// If current element is divisible by k ` ` ` `// multiply with product so far ` ` ` `if` `(arr[i] % k == 0) { ` ` ` `prod *= arr[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return calculated product ` ` ` `return` `prod; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 15, 16, 10, 9, 6, 7, 17 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 3; ` ` ` ` ` `cout << findProduct(arr, n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find Product of all the elements ` `// in an array divisible by a given number K ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to find Product of all the elements ` `// in an array divisible by a given number K ` `static` `int` `findProduct(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `prod = ` `1` `; ` ` ` ` ` `// Traverse the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` ` ` `// If current element is divisible by k ` ` ` `// multiply with product so far ` ` ` `if` `(arr[i] % k == ` `0` `) { ` ` ` `prod *= arr[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return calculated product ` ` ` `return` `prod; ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `arr[] = { ` `15` `, ` `16` `, ` `10` `, ` `9` `, ` `6` `, ` `7` `, ` `17` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `k = ` `3` `; ` ` ` ` ` `System.out.println(findProduct(arr, n, k)); ` ` ` `} ` `} ` ` ` ` ` `// This code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find Product of all ` `# the elements in an array divisible by ` `# a given number K ` ` ` `# Function to find Product of all the elements ` `# in an array divisible by a given number K ` `def` `findProduct(arr, n, k): ` ` ` ` ` `prod ` `=` `1` ` ` ` ` `# Traverse the array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# If current element is divisible ` ` ` `# by k, multiply with product so far ` ` ` `if` `(arr[i] ` `%` `k ` `=` `=` `0` `): ` ` ` `prod ` `*` `=` `arr[i] ` ` ` ` ` `# Return calculated product ` ` ` `return` `prod ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr` `=` `[` `15` `, ` `16` `, ` `10` `, ` `9` `, ` `6` `, ` `7` `, ` `17` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `k ` `=` `3` ` ` ` ` `print` `(findProduct(arr, n, k)) ` ` ` `# This code is contributed by ita_c ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find Product of all ` `// the elements in an array divisible ` `// by a given number K ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find Product of all ` `// the elements in an array divisible ` `// by a given number K ` `static` `int` `findProduct(` `int` `[]arr, ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `prod = 1; ` ` ` ` ` `// Traverse the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// If current element is divisible ` ` ` `// by k multiply with product so far ` ` ` `if` `(arr[i] % k == 0) ` ` ` `{ ` ` ` `prod *= arr[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return calculated product ` ` ` `return` `prod; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]arr = { 15, 16, 10, 9, 6, 7, 17 }; ` ` ` `int` `n = arr.Length; ` ` ` `int` `k = 3; ` ` ` ` ` `Console.WriteLine(findProduct(arr, n, k)); ` `} ` `} ` ` ` `// This code is contributed by inder_verma ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find Product of ` `// all the elements in an array ` `// divisible by a given number K ` ` ` `// Function to find Product of ` `// all the elements in an array ` `// divisible by a given number K ` `function` `findProduct(&` `$arr` `, ` `$n` `, ` `$k` `) ` `{ ` ` ` `$prod` `= 1; ` ` ` ` ` `// Traverse the array ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// If current element is divisible ` ` ` `// by k multiply with product so far ` ` ` `if` `(` `$arr` `[` `$i` `] % ` `$k` `== 0) ` ` ` `{ ` ` ` `$prod` `*= ` `$arr` `[` `$i` `]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return calculated product ` ` ` `return` `$prod` `; ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `(15, 16, 10, 9, 6, 7, 17 ); ` `$n` `= sizeof(` `$arr` `); ` `$k` `= 3; ` ` ` `echo` `(findProduct(` `$arr` `, ` `$n` `, ` `$k` `)); ` ` ` `// This code is contributed ` `// by Shivi_Aggarwal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

810

**Time Complexity**: O(N), where N is the number of elements in the array.

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:

- Maximize product of array by replacing array elements with its sum or product with element from another array
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Product of all nodes in a doubly linked list divisible by a given number K
- Sum of all the elements in an array divisible by a given number K
- Sum and Product of all Composite numbers which are divisible by k in an array
- Make all array elements divisible by a number K
- Find minimum value to assign all array elements so that array product becomes greater
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- 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]
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Count of elements not divisible by any other elements of Array
- Minimum operations required to make all Array elements divisible by K
- Count numbers in a range that are divisible by all array elements
- Smallest perfect square divisible by all elements of an array
- Smallest perfect Cube divisible by all elements of an array
- Minimum steps to make all the elements of the array divisible by 4
- Find an array element such that all elements are divisible by it
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of Array elements greater than all elements on its left and at least K elements on its right

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.