- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Product of all the elements in an array divisible by a given number K in C++

Given an array arr[n] with n number of integers and another integer k, the task is to find the product all the elements of arr[] which are divisible by k.

To solve the problem we have to iterate every element of the array and find whether it is completely divisible by the number k and then product all the elements and store it into a variable. Like we have an array arr[] = {1, 2, 3, 4, 5, 6 } and assuming we have k = 2 so the numbers in the array which are divisible by 2 are 2, 4, 6 and their product will be equal to 48.

So, let's see the example how we want our answers as per the input

**Input**

arr[] = {10, 11, 55, 2, 6, 7} K = 11

**Output**

605

**Explanation** − the numbers divisible by 11 are 11 and 55 only their product is 605

**Input**

arr[] = {9, 8, 7, 6, 3} K = 3

**Output**

162

## Approach used below is as follows to solve the problem

Iterate the whole array till the very end of an array.

Look for every integer which is divisible by K.

Product every element divisible by K.

Return the product.

Print the result.

## Algorithm

Start Step 1→ declare function to find all the numbers divisible by number K int product(int arr[], int size, int k) declare int prod = 1 Loop For int i = 0 and i < size and i++ IF (arr[i] % k == 0) Set prod *= arr[i] End End return prod Step 2→ In main() Declare int arr[] = {2, 3, 4, 5, 6 } Declare int size = sizeof(arr) / sizeof(arr[0]) Set int k = 2 Call product(arr, size, k) Stop

## Example

#include <iostream> using namespace std; //function to find elements in an array divisible by k int product(int arr[], int size, int k){ int prod = 1; for (int i = 0; i < size; i++){ if (arr[i] % k == 0){ prod *= arr[i]; } } return prod; } int main(){ int arr[] = {2, 3, 4, 5, 6 }; int size = sizeof(arr) / sizeof(arr[0]); int k = 2; cout<<"product of elements are : "<<product(arr, size, k); return 0; }

## Output

If run the above code it will generate the following output −

product of elements are : 48

- Related Articles
- Count the number of elements in an array which are divisible by k in C++
- Possible to make a divisible by 3 number using all digits in an array in C++
- Count subarrays whose product is divisible by k in C++
- Minimum steps to make all the elements of the array divisible by 4 in C++
- Count numbers in a range that are divisible by all array elements in C++
- Find an array element such that all elements are divisible by it using c++
- Count all prefixes of the given binary array which are divisible by x in C++
- Check whether product of digits at even places of a number is divisible by K in Python
- Largest K digit number divisible by X in C++
- Maximum and Minimum element of a linked list which is divisible by a given number k in C++
- Elements of an array that are not divisible by any element of another array in C++
- Find elements of an array which are divisible by N using STL in C++
- Find nth number that contains the digit k or divisible by k in C++
- Maximize the number of sum pairs which are divisible by K in C++
- Product of all Subsequences of size K except the minimum and maximum Elements in C++