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 ; Driver Code ; Given array ; Size of the array ; Function call to count perfect numbers having sum of digits also perfect
def isPerfect ( N ) : NEW_LINE INDENT sumOfDivisors = 1 ; NEW_LINE for i in range ( 2 , int ( N / 2 ) + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT sumOfDivisors += i ; NEW_LINE DEDENT DEDENT if ( sumOfDivisors == N ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT def sumOfDigits ( N ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( N != 0 ) : NEW_LINE INDENT sum += ( N % 10 ) ; NEW_LINE N = N // 10 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def countPerfectNumbers ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT if ( isPerfect ( arr [ i ] ) ) : NEW_LINE INDENT sum = sumOfDigits ( arr [ i ] ) ; NEW_LINE if ( isPerfect ( sum ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 8 , 12 , 28 , 6 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE countPerfectNumbers ( arr , N ) ; NEW_LINE DEDENT