Largest number smaller than or equal to n and digits in non | Returns the required number ; loop to recursively check the numbers less than or equal to given number ; Keep traversing digits from right to left . For every digit check if it is smaller than prev_dig ; We found the required number ; Driver Code
< ? php function nondecdigits ( $ n ) { $ x = 0 ; for ( $ x = $ n ; $ x >= 1 ; $ x -- ) { $ no = $ x ; $ prev_dig = 11 ; $ flag = true ; while ( $ no != 0 ) { if ( $ prev_dig < $ no % 10 ) { $ flag = false ; break ; } $ prev_dig = $ no % 10 ; $ no /= 10 ; } if ( $ flag == true ) break ; } return $ x ; } $ n = 200 ; echo nondecdigits ( $ n ) ; ? >