Print all Perfect Numbers from an array whose sum of digits is also a Perfect Number | Function to check if a number is perfect number or not ; Stores sum of proper divisors ; If sum of digits is equal to N , then it 's a perfect number ; Otherwise , not a perfect number ; Function to find the sum of digits of a number ; Stores sum of digits ; Return sum of digits ; Function to count perfect numbers from an array whose sum of digits is also perfect ; Traverse the array ; If number is perfect ; Stores sum of digits of the number ; If that is also perfect number ; Print that number ; Given array ; Size of the array ; Function call to count perfect numbers having sum of digits also perfect
function isPerfect ( N ) { let sumOfDivisors = 1 ; for ( let i = 2 ; i <= Math . floor ( N / 2 ) ; ++ i ) { if ( N % i === 0 ) { sumOfDivisors += i ; } } if ( sumOfDivisors === N ) { return 1 ; } else return 0 ; } function sumOfDigits ( N ) { let sum = 0 ; while ( N != 0 ) { sum += ( N % 10 ) ; N = Math . floor ( N / 10 ) ; } return sum ; } function countPerfectNumbers ( arr , N ) { for ( let i = 0 ; i < N ; ++ i ) { if ( isPerfect ( arr [ i ] ) ) { let sum = sumOfDigits ( arr [ i ] ) ; if ( isPerfect ( sum ) ) { document . write ( arr [ i ] + " " ) ; } } } } let arr = [ 3 , 8 , 12 , 28 , 6 ] ; let N = arr . length ; countPerfectNumbers ( arr , N ) ;