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 program
function digSum ( n ) { let sum = 0 , rem = 0 ; while ( n > 0 ) { rem = n % 10 ; sum += rem ; n = Math . floor ( n / 10 ) ; } return sum ; } function findX ( n ) { for ( let i = 0 ; i <= n ; i ++ ) if ( i + digSum ( i ) == n ) return i ; return - 1 ; } let n = 43 ; document . write ( " " + findX ( n ) ) ;