Find nth number that contains the digit k or divisible by k . | Function for checking if digit k is in n or not ; finding remainder ; if digit found ; Function for finding nth number ; since k is the first which satisfy th criteria , so consider it in count making count = 1 and starting from i = k + 1 ; checking that the number contain k digit or divisible by k ; Driver Code
function checkdigit ( n , k ) { while ( n != 0 ) { let rem = n % 10 ; if ( rem == k ) return true ; n = n / 10 ; } return false ; } function findNthNumber ( n , k ) { for ( let i = k + 1 , count = 1 ; count < n ; i ++ ) { if ( checkdigit ( i , k ) || ( i % k == 0 ) ) count ++ ; if ( count == n ) return i ; } return - 1 ; } let n = 10 , k = 2 ; document . write ( findNthNumber ( n , k ) ) ;