Find Nth even length palindromic number formed using digits X and Y | C # program to find nth even palindromic number of only even length composing of 4 ' s ▁ and ▁ 5' s . ; Utility function to compute n 'th palindrome number  ; Calculate the length from above formula as discussed above ; Calculate rank for length L ; Mask to check if i 't bit   is set or not  ; If bit is set append '5' else append '4' ;  ; Driver Code
using System ; class GFG { static String solve ( int n , char x , char y ) { int length = ( int ) Math . Ceiling ( Math . Log ( n + 2 ) / Math . Log ( 2 ) ) - 1 ; int rank = n - ( 1 << length ) + 1 ; String left = " " , right = " " ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int mask = ( 1 << i ) ; int bit = mask & rank ; if ( bit > 0 ) { left += y ; right += y ; } else { left += x ; right += x ; } } right = reverse ( right ) ; String res = left + right ; return res ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = 0 ; r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { int n = 23 ; char x = '4' , y = '5' ; String ans = solve ( n , x , y ) ; Console . WriteLine ( ans ) ; } }