# Sum 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 sum of all such elements which are divisible by K.

Examples:

```Input : arr[] = {15, 16, 10, 9, 6, 7, 17}
K = 3
Output : 30

Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9}
K = 2
Output : 20
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

Below is the implementation of the above approach:

## C++

 `// C++ program to find sum of all the elements ` `// in an array divisible by a given number K ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find sum of all the elements ` `// in an array divisible by a given number K ` `int` `findSum(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `sum = 0; ` ` `  `    ``// Traverse the array ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// If current element is divisible by k ` `        ``// add it to sum ` `        ``if` `(arr[i] % k == 0) { ` `            ``sum += arr[i]; ` `        ``} ` `    ``} ` ` `  `    ``// Return calculated sum ` `    ``return` `sum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 15, 16, 10, 9, 6, 7, 17 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `k = 3; ` ` `  `    ``cout << findSum(arr, n, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find sum of all the elements ` `// in an array divisible by a given number K ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `// Function to find sum of all the elements ` `// in an array divisible by a given number K ` `static` `int` `findSum(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `sum = ``0``; ` ` `  `    ``// Traverse the array ` `    ``for` `(``int` `i = ``0``; i < n; i++) { ` ` `  `        ``// If current element is divisible by k ` `        ``// add it to sum ` `        ``if` `(arr[i] % k == ``0``) { ` `            ``sum += arr[i]; ` `        ``} ` `    ``} ` ` `  `    ``// Return calculated sum ` `    ``return` `sum; ` `} ` ` `  `    ``// 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( findSum(arr, n, k)); ` `    ``} ` `} ` ` `  `// this code is contributed by anuj_67.. `

## Python3

 `# Python3 program to find sum of  ` `# all the elements in an array ` `# divisible by a given number K  ` ` `  `# Function to find sum of all  ` `# the elements in an array  ` `# divisible by a given number K ` `def` `findSum(arr, n, k) : ` ` `  `    ``sum` `=` `0` ` `  `    ``# Traverse the array  ` `    ``for` `i ``in` `range``(n) : ` ` `  `        ``# If current element is divisible  ` `        ``# by k add it to sum  ` `        ``if` `arr[i] ``%` `k ``=``=` `0` `: ` ` `  `            ``sum` `+``=` `arr[i] ` ` `  `    ``# Return calculated sum  ` `    ``return` `sum` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``arr ``=` `[ ``15``, ``16``, ``10``, ``9``, ``6``, ``7``, ``17``] ` `    ``n ``=` `len``(arr) ` `    ``k ``=` `3` ` `  `    ``print``(findSum(arr, n, k)) ` ` `  `# This code is contributed by ANKITRAI1 `

## C#

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

## PHP

 `

Output:

```30
```

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

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : vt_m, AnkitRai01, Akanksha_Rai