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 is equal to n . ; if no such i found return - 1 ; Driver Code
< ? php function digSum ( $ n ) { $ sum = 0 ; $ rem = 0 ; while ( $ n ) { $ rem = $ n % 10 ; $ sum += $ rem ; $ n /= 10 ; } return $ sum ; } function findX ( $ n ) { for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) if ( $ i + digSum ( $ i ) == $ n ) return $ i ; return -1 ; } $ n = 43 ; echo " x = " ? >