N 'th palindrome of K digits | A naive approach of C ++ program of finding nth palindrome of k digit ; Utility function to reverse the number n ; Boolean Function to check for palindromic number ; Function for finding nth palindrome of k digits ; Get the smallest k digit number ; check the number is palindrom or not ; if n 'th palindrome found break the loop ; Increment number for checking next palindrome ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int reverseNum ( int n ) { int rem , rev = 0 ; while ( n ) { rem = n % 10 ; rev = rev * 10 + rem ; n /= 10 ; } return rev ; } bool isPalindrom ( int num ) { return num == reverseNum ( num ) ; } int nthPalindrome ( int n , int k ) { int num = ( int ) pow ( 10 , k - 1 ) ; while ( true ) { if ( isPalindrom ( num ) ) -- n ; if ( ! n ) break ; ++ num ; } return num ; } int main ( ) { int n = 6 , k = 5 ; printf ( " % dth ▁ palindrome ▁ of ▁ % d ▁ digit ▁ = ▁ % d STRNEWLINE " , n , k , nthPalindrome ( n , k ) ) ; n = 10 , k = 6 ; printf ( " % dth ▁ palindrome ▁ of ▁ % d ▁ digit ▁ = ▁ % d " , n , k , nthPalindrome ( n , k ) ) ; return 0 ; }