Largest even digit number not greater than N | function to check if all digits are even of a given number ; iterate for all digits ; if digit is odd ; all digits are even ; function to return the largest number with all digits even ; iterate till we find a number with all digits even ; Driver Code
function checkDigits ( n ) { while ( n > 0 ) { if ( ( ( n % 10 ) % 2 ) > 0 ) return 0 ; n = parseInt ( n / 10 ) ; } return 1 ; } function largestNumber ( n ) { for ( i = n ; ; i -- ) if ( checkDigits ( i ) > 0 ) return i ; } var N = 23 ; document . write ( largestNumber ( N ) ) ;