Count array elements having sum of digits equal to K | Function to calculate the sum of digits of the number N ; Stores the sum of digits ; Return the sum ; Function to count array elements ; Store the count of array elements having sum of digits K ; Traverse the array ; If sum of digits is equal to K ; Increment the count ; Print the count ; Given array ; Given value of K ; Size of the array ; Function call to count array elements having sum of digits equal to K
function sumOfDigits ( N ) { let sum = 0 ; while ( N != 0 ) { sum += N % 10 ; N = parseInt ( N / 10 , 10 ) ; } return sum ; } function elementsHavingDigitSumK ( arr , N , K ) { let count = 0 ; for ( let i = 0 ; i < N ; ++ i ) { if ( sumOfDigits ( arr [ i ] ) == K ) { count ++ ; } } document . write ( count ) ; } let arr = [ 23 , 54 , 87 , 29 , 92 , 62 ] ; let K = 11 ; let N = arr . length ; elementsHavingDigitSumK ( arr , N , K ) ;