Find all palindrome numbers of given digits
Last Updated :
20 Nov, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int reverse( int num)
{
int rev = 0;
while (num > 0) {
rev = rev * 10 + num % 10;
num = num / 10;
}
return rev;
}
bool isPalindrome( int num)
{
if (num == reverse(num))
return true ;
return false ;
}
void printPalindromes( int d)
{
if (d <= 0)
return ;
int smallest = pow (10, d - 1);
int largest = pow (10, d) - 1;
for ( int i = smallest;
i <= largest; i++) {
if (isPalindrome(i))
cout << i << " " ;
}
}
int main()
{
int d = 2;
printPalindromes(d);
return 0;
}
|
Java
class GFG
{
static int reverse( int num)
{
int rev = 0 ;
while (num > 0 )
{
rev = rev * 10 + num % 10 ;
num = num / 10 ;
}
return rev;
}
static boolean isPalindrome( int num)
{
if (num == reverse(num))
return true ;
return false ;
}
static void printPalindromes( int d)
{
if (d <= 0 )
return ;
int smallest = ( int )Math.pow( 10 , d - 1 );
int largest = ( int )Math.pow( 10 , d) - 1 ;
for ( int i = smallest; i <= largest; i++)
{
if (isPalindrome(i))
System.out.print(i + " " );
}
}
public static void main (String[] args)
{
int d = 2 ;
printPalindromes(d);
}
}
|
Python3
def reverse(num):
rev = 0 ;
while (num > 0 ):
rev = rev * 10 + num % 10 ;
num = num / / 10 ;
return rev;
def isPalindrome(num):
if (num = = reverse(num)):
return True ;
return False ;
def printPalindromes(d):
if (d < = 0 ):
return ;
smallest = pow ( 10 , d - 1 );
largest = pow ( 10 , d) - 1 ;
for i in range (smallest, largest + 1 ):
if (isPalindrome(i)):
print (i, end = " " );
d = 2 ;
printPalindromes(d);
|
C#
using System;
class GFG
{
static int reverse( int num)
{
int rev = 0;
while (num > 0)
{
rev = rev * 10 + num % 10;
num = num / 10;
}
return rev;
}
static bool isPalindrome( int num)
{
if (num == reverse(num))
return true ;
return false ;
}
static void printPalindromes( int d)
{
if (d <= 0)
return ;
int smallest = ( int )Math.Pow(10, d - 1);
int largest = ( int )Math.Pow(10, d) - 1;
for ( int i = smallest; i <= largest; i++)
{
if (isPalindrome(i))
Console.Write(i + " " );
}
}
public static void Main (String[] args)
{
int d = 2;
printPalindromes(d);
}
}
|
Javascript
<script>
function reverse(num)
{
let rev = 0;
while (num > 0)
{
rev = rev * 10 + num % 10;
num = parseInt(num / 10, 10);
}
return rev;
}
function isPalindrome(num)
{
if (num == reverse(num))
return true ;
return false ;
}
function printPalindromes(d)
{
if (d <= 0)
return ;
let smallest = Math.pow(10, d - 1);
let largest = Math.pow(10, d) - 1;
for (let i = smallest; i <= largest; i++)
{
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
Share your thoughts in the comments
Please Login to comment...