Sum of all the elements in an array divisible by a given number K - GeeksforGeeks

03 Aug, 2021
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
Explanation: As 15, 9, 6 are divisible by 3. So, sum of elements divisible by K = 15 + 9 + 6 = 30.

Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9}
        K = 2
Output : 20
 
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++
Java
Python3
C#
PHP
Javascript
// C++ program to find sum of all the elements// in an array divisible by a given number K #include <iostream>using namespace std; // Function to find sum of all the elements// in an array divisible by a given number Kint 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 codeint main(){    int arr[] = { 15, 16, 10, 9, 6, 7, 17 };    int n = sizeof(arr) / sizeof(arr[0]);    int k = 3;     cout << findSum(arr, n, k);     return 0;}
// 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 Kstatic 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 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 Kdef 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 codeif __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# 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 Kstatic 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// PHP 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 Kfunction findSum($arr, $n, $k){    $sum = 0;     // Traverse the array    for ($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$arr = array(15, 16, 10, 9, 6, 7, 17);$n = sizeof($arr);$k = 3; echo findSum($arr, $n, $k); // This code is contributed// by Akanksha Rai(Abby_akku)
<script>    // Javascript 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    function findSum(arr, n, k)    {        let sum = 0;         // Traverse the array        for (let 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;    }         let arr = [ 15, 16, 10, 9, 6, 7, 17 ];    let n = arr.length;    let k = 3;       document.write(findSum(arr, n, k)); </script>
30
 
Time Complexity: O(N), where N is the number of elements in the array.Auxiliary Space: O(1)  
vt_m
ankthon
Akanksha_Rai
decode2207
pankajsharmagfg
Technical Scripter 2018
Arrays
Data Structures
Data Structures
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
SDE SHEET - A Complete Guide for SDE Preparation
Top 50 Array Coding Problems for Interviews
DSA Sheet by Love Babbar
Doubly Linked List | Set 1 (Introduction and Insertion)
Implementing a Linked List in Java using Class