Print all Good numbers in given range | Function to check whether n is a good number and doesn ' t ▁ contain ▁ digit ▁ ' d ' ; Get last digit and initialize sum from right side ; If last digit is d , return ; Traverse remaining digits ; Current digit ; If digit is d or digit is less than or equal to sum of digits on right side ; Update sum and n ; Print Good numbers in range [ L , R ] ; Traverse all numbers in given range ; If current numbers is good , prvar it ; Driver code ; Print good numbers in [ L , R ]
function isValid ( n , d ) { var digit = n % 10 ; var sum = digit ; if ( digit == d ) return false ; n = parseInt ( n / 10 ) ; while ( n > 0 ) { digit = n % 10 ; if ( digit == d digit <= sum ) return false ; else { sum += digit ; n = parseInt ( n / 10 ) ; } } return true ; } function printGoodNumber ( L , R , d ) { for ( i = L ; i <= R ; i ++ ) { if ( isValid ( i , d ) ) document . write ( i + " " ) ; } } var L = 410 , R = 520 , d = 3 ; printGoodNumber ( L , R , d ) ;