N 'th palindrome of K digits | C ++ program of finding nth palindrome of k digit ; Determine the first half digits ; Print the first half digits of palindrome ; If k is odd , truncate the last digit ; print the last half digits of palindrome ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nthPalindrome ( int n , int k ) { int temp = ( k & 1 ) ? ( k / 2 ) : ( k / 2 - 1 ) ; int palindrome = ( int ) pow ( 10 , temp ) ; palindrome += n - 1 ; printf ( " % d " , palindrome ) ; if ( k & 1 ) palindrome /= 10 ; while ( palindrome ) { printf ( " % d " , palindrome % 10 ) ; palindrome /= 10 ; } printf ( " STRNEWLINE " ) ; } int main ( ) { int n = 6 , k = 5 ; printf ( " % dth ▁ palindrome ▁ of ▁ % d ▁ digit ▁ = ▁ " , n , k ) ; nthPalindrome ( n , k ) ; n = 10 , k = 6 ; printf ( " % dth ▁ palindrome ▁ of ▁ % d ▁ digit ▁ = ▁ " , n , k ) ; nthPalindrome ( n , k ) ; return 0 ; }