Largest number not greater than N all the digits of which are odd | Function to check if all digits of a number are odd ; iterate for all digits ; if digit is even ; all digits are odd ; function to return the largest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code
< ? php function allOddDigits ( $ n ) { while ( $ n > 1 ) { if ( ( $ n % 10 ) % 2 == 0 ) return false ; $ n = ( int ) $ n / 10 ; } return true ; } function largestNumber ( $ n ) { if ( $ n % 2 == 0 ) $ n -- ; for ( $ i = $ n ; ; $ i = ( $ i - 2 ) ) if ( allOddDigits ( $ i ) ) return $ i ; } $ N = 23 ; echo largestNumber ( $ N ) ; ? >