Find a number x such that sum of x and its digits is equal to given n . | utility function for digit sum ; function for finding x ; iterate from 1 to n . For every no . check if its digit sum with it isequal to n . ; if no such i found return - 1 ; Driver Code
def digSum ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE rem = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT rem = n % 10 ; NEW_LINE sum = sum + rem ; NEW_LINE n = int ( n / 10 ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def findX ( n ) : NEW_LINE INDENT for i in range ( n + 1 ) : NEW_LINE INDENT if ( i + digSum ( i ) == n ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT n = 43 ; NEW_LINE print ( " x ▁ = ▁ " , findX ( n ) ) ; NEW_LINE