Sum of array elements which are multiples of a given number | Java program for the above approach ; Function to find the sum of array elements which are multiples of N ; Stores the sum ; Traverse the given array ; If current element is a multiple of N ; Print total sum ; Driver Code ; Given arr [ ]
import java . io . * ; import java . util . * ; class GFG { static void mulsum ( int arr [ ] , int n , int N ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % N == 0 ) { sum = sum + arr [ i ] ; } } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 5 , 6 } ; int n = arr . length ; int N = 3 ; mulsum ( arr , n , N ) ; } }