Generate all palindromic numbers less than n

Find all numbers less than n, which are palindromic. Numbers can be printed in any order.

Examples :

Input : n = 12
Output : 1, 2, 3, 4, 5, 6, 7, 8, 9, 11

Input : n = 104
Output : 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 
         22, 33, 44, 55, 66, 77, 88, 99, 101
[Note that below program prints these numbers
 in different 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.

C/C++



filter_none

edit
close

play_arrow

link
brightness_4
code

// A C++ program to generate palindromic numbers
// less than n.
#include <iostream>
using namespace std;
  
// A utility for creating palindrome
int 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;
}
  
// Function to print decimal palindromic number
void generatePalindromes(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++;
        }
    }
}
  
// Driver Program to test above function
int main()
{
    int n = 104;
    generatePalindromes(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to generate palindromic
// numbers less than n.
class GFG {
  
// A utility for creating palindrome
static int createPalindrome(int input, int b, int 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 == 1)
        n /= b;
  
    // Creates palindrome by just appending reverse
    // of number to itself
    while (n > 0) {
        palin = palin * b + (n % b);
        n /= b;
    }
    return palin;
}
  
// Function to print decimal 
// palindromic number
static void generatePalindromes(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) {
            System.out.print(number + " ");
            i++;
    }
    }
}
  
// Driver code
public static void main(String[] args) {
    int n = 104;
    generatePalindromes(n);
}
}
// This code is contributed by Anant Agarwal.

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Generate all palindromic numbers less than n
# A Python program to generate palindromic numbers 
# less than n.
def createPalindrome(inp, b, isOdd):
    n = inp
    palin = inp
   
    # 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 = n / b
   
    # Creates palindrome by just appending reverse
    # of number to itself
    while (n > 0):
        palin = palin * b + (n % b)
        n = n / b
    return palin
   
# Function to print decimal palindromic number
def generatePalindromes(n):
   
    # Run two times for odd and even length palindromes
    for j in range(2):
        # Creates palindrome numbers with first half as i. 
        # Value of j decided whether we need an odd length
        # of even length palindrome.
        i = 1
        while (createPalindrome(i, 10, j % 2) < n):
            print createPalindrome(i, 10, j % 2),
            i = i + 1
   
# Driver Program to test above function
n = 104
generatePalindromes(n)
  
#This code is contributed by Afzal Ansari

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A C# program to generate palindromic
// numbers less than n.
using System;
  
class GFG {
  
// A utility for creating palindrome
static int createPalindrome(int input, int b,
                            int 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 == 1)
        n /= b;
  
    // Creates palindrome by just appending 
    // reverse of number to itself
    while (n > 0)
    {
        palin = palin * b + (n % b);
        n /= b;
    }
    return palin;
}
  
// Function to print decimal 
// palindromic number
static void generatePalindromes(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)
        {
            Console.Write(number + " ");
            i++;
    }
    }
}
  
// Driver Code
public static void Main() 
{
    int n = 104;
    generatePalindromes(n);
}
}
  
// This code is contributed by Nitin Mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to generate 
// palindromic numbers less than n.
  
// A utility function for 
// creating palindrome
function createPalindrome($input
                          $b, $isOdd)
{
    $n = $input;
    $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 = intval($n / $b);
  
    // Creates palindrome by just appending 
    // reverse of number to itself
    while ($n > 0)
    {
        $palin = $palin * $b + intval($n % $b);
        $n = intval($n / $b);
    }
    return $palin;
}
  
// Function to print decimal
// palindromic number
function generatePalindromes($n)
{
    $number = 0;
  
    // Run two times for odd and
    // even length palindromes
    for ($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.
        $i = 1;
        while (($number
                   createPalindrome($i, 10, 
                                    $j % 2)) < $n)
        {
            echo $number . " ";
            $i++;
        }
    }
}
  
// Driver Code
$n = 104;
generatePalindromes($n);
      
// This code is contributed by Sam007
?>

chevron_right



Output :

11 22 33 44 55 66 77 88 99 1 2 3 4 5 6 7 8 9 101 

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.

This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up