Sum of array elements which are multiples of a given number

04 May, 2021
Given an array arr[] consisting of positive integers and an integer N, the task is to find the sum of all array elements which are multiples of N
Examples:
Input: arr[] = {1, 2, 3, 5, 6}, N = 3Output: 9Explanation: From the given array, 3 and 6 are multiples of 3. Therefore, sum = 3 + 6 = 9.
Input: arr[] = {1, 2, 3, 5, 7, 11, 13}, N = 5Output: 5
Approach: The idea is to traverse the array and for each array element, check if it is a multiple of N or not and add those elements. Follow the steps below to solve the problem:
Initialize a variable, say sum, to store the required sum.Traverse the given array and for each array element, perform the following operations.Check whether the array element is a multiple of N or not.If the element is a multiple of N, then add the element to sum.Finally, print the value of sum.
Initialize a variable, say sum, to store the required sum.
Traverse the given array and for each array element, perform the following operations.
Check whether the array element is a multiple of N or not.
If the element is a multiple of N, then add the element to sum.
Finally, print the value of sum.
Below is the implementation of the above approach:
C++
Java
Python
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the sum of array// elements which are multiples of Nvoid mulsum(int arr[], int n, int N){     // Stores the sum    int sum = 0;     // Traverse the given array    for (int i = 0; i < n; i++) {         // If current element        // is a multiple of N        if (arr[i] % N == 0) {            sum = sum + arr[i];        }    }     // Print total sum    cout << sum;} // Driver Codeint main(){     // Given arr[]    int arr[] = { 1, 2, 3, 5, 6 };     int n = sizeof(arr) / sizeof(arr[0]);     int N = 3;     mulsum(arr, n, N);     return 0;}
// Java program for the above approachimport java.io.*;import java.util.*;class GFG{ // Function to find the sum of array// elements which are multiples of Nstatic void mulsum(int arr[], int n, int N){     // Stores the sum    int sum = 0;     // Traverse the given array    for (int i = 0; i < n; i++)    {         // If current element        // is a multiple of N        if (arr[i] % N == 0)        {            sum = sum + arr[i];        }    }     // Print total sum    System.out.println(sum);}  // Driver Codepublic static void main(String[] args){         // Given arr[]    int arr[] = { 1, 2, 3, 5, 6 };    int n = arr.length;    int N = 3;    mulsum(arr, n, N);}} // This code is contributed by jana_sayantan.
# Python3 program for the above approach  # Function to find the sum of array# elements which are multiples of Ndef mulsum(arr, n, N):          # Stores the sum    sums = 0      # Traverse the array    for i in range(0, n):        if arr[i] % N == 0:              sums = sums + arr[i]      # Print total sum    print(sums)  # Driver Codeif __name__ == "__main__":      # Given arr[]    arr = [ 1, 2, 3, 5, 6 ]      n = len(arr)         N = 3      # Function call    mulsum(arr, n, N)
// C# program for the above approachusing System;public class GFG{ // Function to find the sum of array// elements which are multiples of Nstatic void mulsum(int[] arr, int n, int N){     // Stores the sum    int sum = 0;     // Traverse the given array    for (int i = 0; i < n; i++)    {         // If current element        // is a multiple of N        if (arr[i] % N == 0)        {            sum = sum + arr[i];        }    }     // Print total sum    Console.Write(sum);} // Driver Codestatic public void Main (){    // Given arr[]    int[] arr = { 1, 2, 3, 5, 6 };    int n = arr.Length;    int N = 3;    mulsum(arr, n, N);}} // This code is contributed by Dharanendra L V.
<script> // JavaScript program for the above approach // Function to find the sum of array// elements which are multiples of Nfunction mulsum(arr, n, N){         // Stores the sum    var sum = 0;         // Traverse the given array    for(var i = 0; i < n; i++)    {                 // If current element        // is a multiple of N        if (arr[i] % N == 0)        {            sum = sum + arr[i];        }    }         // Print total sum    document.write(sum);} // Driver Code // Given arr[]var arr = [ 1, 2, 3, 5, 6 ];var n = arr.length;var N = 3; mulsum(arr, n, N); // This code is contributed by rdtank </script>
9
 
Time Complexity: O(N)Auxiliary Space: O(1)
jana_sayantan
dharanendralv23
rdtank
divisibility
Technical Scripter 2020
Arrays
Mathematical
Searching
Technical Scripter
Arrays
Searching
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.