http://www.geeksforgeeks.org/generate-palindromic-numbers-less-n/
Find all numbers less than n, which are palindromic. Numbers can be printed in any order.
Brute Force: We check all the numbers from 1 to n whether its decimal representation is palindrome or not.
Efficient Approach: We start from 1 and create palindromes of odd digit and even digits up to n. For every number (starting from 1), we append its reverse at end if we need even length palindrome numbers. For odd length palindrome, we append reverse of all digits except last one.
Note that the above program doesn’t print output in sorted order. To print in sorted order, we can store palindromes in a vector and store it.
// A utility for creating palindromeint createPalindrome(int input, int b, bool isOdd){ int n = input; int palin = input; // checks if number of digits is odd or even // if odd then neglect the last digit of input in // finding reverse as in case of odd number of // digits middle element occur once if (isOdd) n /= b; // Creates palindrome by just appending reverse // of number to itself while (n > 0) { palin = palin * b + (n % b); n /= b; } return palin;}// Fruition to print decimal palindromic numbervoid generatePaldindromes(int n){ int number; // Run two times for odd and even length palindromes for (int j = 0; j < 2; j++) { // Creates palindrome numbers with first half as i. // Value of j decided whether we need an odd length // of even length palindrome. int i = 1; while ((number = createPalindrome(i, 10, j % 2)) < n) { cout << number << " "; i++; } }}