Largest even digit number not greater than N | C # program to print the largest integer not greater than N with all even digits ; function to return the largest number with all digits even ; convert the number to a string for easy operations ; find first odd digit ; if no digit , then N is the answer ; till first odd digit , add all even numbers ; decrease 1 from the odd digit ; add 0 in the rest of the digits ; Driver Code
using System ; class GFG { static int largestNumber ( int n ) { string s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ( ( ( int ) ( s [ i ] - '0' ) % 2 ) & 1 ) > 0 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; int num = 0 ; for ( int i = 0 ; i < index ; i ++ ) num = num * 10 + ( int ) ( s [ i ] - '0' ) ; num = num * 10 + ( ( int ) s [ index ] - ( int ) ( '0' ) - 1 ) ; for ( int i = index + 1 ; i < s . Length ; i ++ ) num = num * 10 + 8 ; return num ; } static void Main ( ) { int N = 24578 ; Console . WriteLine ( largestNumber ( N ) ) ; } }