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

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 Kint 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 codeint 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;} |
C
// C program to find Product of all the elements// in an array divisible by a given number K#include <stdio.h>Â
// Function to find Product of all the elements// in an array divisible by a given number Kint 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 codeint main(){Â Â Â Â int arr[] = { 15, 16, 10, 9, 6, 7, 17 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â Â Â Â int k = 3;Â Â Â Â printf("%d",findProduct(arr, n, k));Â
    return 0;}Â
// This code is contributed by kothavvsaakash. |
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 Kstatic 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.. |
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 Kdef 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 codeif __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 |
C#
// C# program to find Product of all // the elements in an array divisible// by a given number Kusing System;Â
class GFG {Â
// Function to find Product of all // the elements in an array divisible// by a given number Kstatic 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 codepublic 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 |
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?> |
Javascript
<script>// Function to find Product of all the elements// in an array divisible by a given number Kfunction findProduct( arr, n, k){    var prod = 1;Â
    // Traverse the array    for (var 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;}Â
var arr = [15, 16, 10, 9, 6, 7, 17 ];Â Â Â Â Â Â
    document.write(findProduct(arr, 7, 3));Â
Â
Â
</script> |
Output
810
Complexity Analysis:
- Time Complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(1)Â
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



