Open In App

Find all palindrome numbers of given digits

Last Updated : 20 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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;
}


Java




// 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


Python3




# 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 print all 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


C#




// 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


Javascript




<script>
 
    // JavaScript implementation of the approach
     
    // Function to return the
    // reverse of num
    function reverse(num)
    {
        let rev = 0;
        while (num > 0)
        {
            rev = rev * 10 + num % 10;
            num = parseInt(num / 10, 10);
        }
        return rev;
    }
       
    // Function that returns true
    // if num is palindrome
    function 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 print all the
    // d-digit palindrome numbers
    function printPalindromes(d)
    {
        if (d <= 0)
            return;
       
        // Smallest and the largest d-digit numbers
        let smallest = Math.pow(10, d - 1);
        let largest = Math.pow(10, d) - 1;
       
        // Starting from the smallest d-digit
        // number till the largest
        for (let i = smallest; i <= largest; i++)
        {
   
            // If the current number
            // is palindrome
            if (isPalindrome(i))
                document.write(i + " ");
        }
    }
     
    let d = 2;
       
      printPalindromes(d);
             
</script>


Output

11 22 33 44 55 66 77 88 99

Time Complexity: O(10^D)

Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads