Find all palindrome numbers of given digits

Given an integer D, the task is to find all the D-digit palindrome numbers.

Examples:

Input: D = 1
Output: 1 2 3 4 5 6 7 8 9

Input: D = 2
Output: 11 22 33 44 55 66 77 88 99

Approach: Numbers with D-digits start from 10(D – 1) to 10D – 1. So, start checking every number from this interval whether it is palindrome or not.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the
// reverse of num
int reverse(int num)
{
    int rev = 0;
    while (num > 0) {
        rev = rev * 10 + num % 10;
        num = num / 10;
    }
    return rev;
}
  
// Function that returns true
// if num is palindrome
bool isPalindrome(int num)
{
  
    // If the number is equal to the
    // reverse of it then it
    // is a palindrome
    if (num == reverse(num))
        return true;
  
    return false;
}
  
// Function to print all the
// d-digit palindrome numbers
void printPalindromes(int d)
{
    if (d <= 0)
        return;
  
    // Smallest and the largest d-digit numbers
    int smallest = pow(10, d - 1);
    int largest = pow(10, d) - 1;
  
    // Starting from the smallest d-digit
    // number till the largest
    for (int i = smallest;
         i <= largest; i++) {
  
        // If the current number
        // is palindrome
        if (isPalindrome(i))
            cout << i << " ";
    }
}
  
// Driver code
int main()
{
    int d = 2;
  
    printPalindromes(d);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
{
      
    // Function to return the 
    // reverse of num 
    static int reverse(int num) 
    
        int rev = 0
        while (num > 0
        
            rev = rev * 10 + num % 10
            num = num / 10
        
        return rev; 
    
      
    // Function that returns true 
    // if num is palindrome 
    static boolean isPalindrome(int num) 
    
      
        // If the number is equal to the 
        // reverse of it then it 
        // is a palindrome 
        if (num == reverse(num)) 
            return true
      
        return false
    
      
    // Function to print all the 
    // d-digit palindrome numbers 
    static void printPalindromes(int d) 
    
        if (d <= 0
            return
      
        // Smallest and the largest d-digit numbers 
        int smallest = (int)Math.pow(10, d - 1); 
        int largest = (int)Math.pow(10, d) - 1
      
        // Starting from the smallest d-digit 
        // number till the largest 
        for (int i = smallest; i <= largest; i++)
        
  
            // If the current number 
            // is palindrome 
            if (isPalindrome(i)) 
                System.out.print(i + " "); 
        
    
      
    // Driver code 
    public static void main (String[] args)
    
        int d = 2
      
        printPalindromes(d); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the approach
  
# Function to return the
# reverse of num
def reverse(num):
    rev = 0;
    while (num > 0):
        rev = rev * 10 + num % 10;
        num = num // 10;
  
    return rev;
  
# Function that returns true
# if num is palindrome
def isPalindrome(num): 
    # If the number is equal to the
    # reverse of it then it
    # is a palindrome
    if (num == reverse(num)):
        return True;
  
    return False;
  
# Function to prall the
# d-digit palindrome numbers
def printPalindromes(d):
  
    if (d <= 0):
        return;
  
    # Smallest and the largest d-digit numbers
    smallest = pow(10, d - 1);
    largest = pow(10, d) - 1;
  
    # Starting from the smallest d-digit
    # number till the largest
    for i in range(smallest, largest + 1):
  
        # If the current number
        # is palindrome
        if (isPalindrome(i)):
            print(i, end = " ");
  
# Driver code
d = 2;
  
printPalindromes(d);
  
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
      
class GFG 
{
      
    // Function to return the 
    // reverse of num 
    static int reverse(int num) 
    
        int rev = 0; 
        while (num > 0) 
        
            rev = rev * 10 + num % 10; 
            num = num / 10; 
        
        return rev; 
    
      
    // Function that returns true 
    // if num is palindrome 
    static bool isPalindrome(int num) 
    
      
        // If the number is equal to the 
        // reverse of it then it 
        // is a palindrome 
        if (num == reverse(num)) 
            return true
      
        return false
    
      
    // Function to print all the 
    // d-digit palindrome numbers 
    static void printPalindromes(int d) 
    
        if (d <= 0) 
            return
      
        // Smallest and the largest d-digit numbers 
        int smallest = (int)Math.Pow(10, d - 1); 
        int largest = (int)Math.Pow(10, d) - 1; 
      
        // Starting from the smallest d-digit 
        // number till the largest 
        for (int i = smallest; i <= largest; i++)
        
  
            // If the current number 
            // is palindrome 
            if (isPalindrome(i)) 
                Console.Write(i + " "); 
        
    
      
    // Driver code 
    public static void Main (String[] args)
    
        int d = 2; 
      
        printPalindromes(d); 
    
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

11 22 33 44 55 66 77 88 99

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.