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++
#include <iostream>
using namespace std;
int createPalindrome( int input, int b, bool isOdd)
{
int n = input;
int palin = input;
if (isOdd)
n /= b;
while (n > 0)
{
palin = palin * b + (n % b);
n /= b;
}
return palin;
}
void generatePalindromes( int n)
{
int number;
for ( int j = 0; j < 2; j++)
{
int i = 1;
while ((number = createPalindrome(i, 10, j % 2)) < n)
{
cout << number << " " ;
i++;
}
}
}
int main()
{
int n = 104;
generatePalindromes(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int createPalindrome( int input, int b, int isOdd) {
int n = input;
int palin = input;
if (isOdd == 1 )
n /= b;
while (n > 0 ) {
palin = palin * b + (n % b);
n /= b;
}
return palin;
}
static void generatePalindromes( int n) {
int number;
for ( int j = 0 ; j < 2 ; j++) {
int i = 1 ;
while ((number = createPalindrome(i, 10 , j % 2 )) < n) {
System.out.print(number + " " );
i++;
}
}
}
public static void main(String[] args) {
int n = 104 ;
generatePalindromes(n);
}
}
|
Python3
def createPalindrome(inp, b, isOdd):
n = inp
palin = inp
if (isOdd):
n = n / / b
while (n > 0 ):
palin = palin * b + (n % b)
n = n / / b
return palin
def generatePalindromes(n):
for j in range ( 2 ):
i = 1
while (createPalindrome(i, 10 , j % 2 ) < n):
print (createPalindrome(i, 10 , j % 2 ),end = " " )
i = i + 1
n = 104
generatePalindromes(n)
|
C#
using System;
class GFG {
static int createPalindrome( int input, int b,
int isOdd)
{
int n = input;
int palin = input;
if (isOdd == 1)
n /= b;
while (n > 0)
{
palin = palin * b + (n % b);
n /= b;
}
return palin;
}
static void generatePalindromes( int n)
{
int number;
for ( int j = 0; j < 2; j++)
{
int i = 1;
while ((number = createPalindrome(i, 10,
j % 2)) < n)
{
Console.Write(number + " " );
i++;
}
}
}
public static void Main()
{
int n = 104;
generatePalindromes(n);
}
}
|
Javascript
<script>
function createPalindrome(input, b, isOdd)
{
let n = input;
let palin = input;
if (isOdd == 1)
n = parseInt(n / b, 10);
while (n > 0)
{
palin = palin * b + (n % b);
n = parseInt(n / b, 10);
}
return palin;
}
function generatePalindromes(n)
{
let number;
for (let j = 0; j < 2; j++)
{
let i = 1;
while ((number = createPalindrome(i, 10, j % 2)) < n)
{
document.write(number + " " );
i++;
}
}
}
let n = 104;
generatePalindromes(n);
</script>
|
PHP
<?php
function createPalindrome( $input ,
$b , $isOdd )
{
$n = $input ;
$palin = $input ;
if ( $isOdd )
$n = intval ( $n / $b );
while ( $n > 0)
{
$palin = $palin * $b + intval ( $n % $b );
$n = intval ( $n / $b );
}
return $palin ;
}
function generatePalindromes( $n )
{
$number = 0;
for ( $j = 0; $j < 2; $j ++)
{
$i = 1;
while (( $number =
createPalindrome( $i , 10,
$j % 2)) < $n )
{
echo $number . " " ;
$i ++;
}
}
}
$n = 104;
generatePalindromes( $n );
?>
|
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 sort it, and don’t forget to use the required header file.
Approach:
- First, we define a function isPalindrome() which takes an integer n as input and returns a boolean value indicating whether it is palindrome or not.
- To check if the number is palindrome, we convert it to a string using to_string() function, then we iterate over its first half and compare it with the reversed second half.
- If the first half is not equal to the reversed second half, the function returns false. Otherwise, it returns true.
- Next, we define the function generatePalindromes() which takes an integer n as input and generates and prints all palindromic numbers less than n.
- We iterate over all numbers from 1 to n-1 using a for loop and check if each number is palindrome or not using the isPalindrome() function.
- If a number is palindrome, we print it using the cout statement.
- Finally, we call the generatePalindromes() function with the given value of n to print all palindromic numbers less than n.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome( int n) {
string str = to_string(n);
int len = str.length();
for ( int i = 0; i < len/2; i++) {
if (str[i] != str[len-1-i])
return false ;
}
return true ;
}
void generatePalindromes( int n) {
for ( int i = 1; i < n; i++) {
if (isPalindrome(i))
cout << i << " " ;
}
}
int main() {
int n = 104;
generatePalindromes(n);
return 0;
}
|
Java
public class PalindromeGenerator {
public static boolean isPalindrome( int n) {
String str = Integer.toString(n);
int len = str.length();
for ( int i = 0 ; i < len / 2 ; i++) {
if (str.charAt(i) != str.charAt(len - 1 - i))
return false ;
}
return true ;
}
public static void generatePalindromes( int n) {
for ( int i = 1 ; i < n; i++) {
if (isPalindrome(i))
System.out.print(i + " " );
}
}
public static void main(String[] args) {
int n = 104 ;
generatePalindromes(n);
}
}
|
Python3
def is_palindrome(n):
str_n = str (n)
length = len (str_n)
for i in range (length / / 2 ):
if str_n[i] ! = str_n[length - 1 - i]:
return False
return True
def generate_palindromes(n):
for i in range ( 1 , n):
if is_palindrome(i):
print (i, end = ' ' )
if __name__ = = "__main__" :
n = 104
generate_palindromes(n)
|
C#
using System;
public class PalindromeGenerator
{
public static bool IsPalindrome( int n)
{
string str = n.ToString();
int len = str.Length;
for ( int i = 0; i < len / 2; i++)
{
if (str[i] != str[len - 1 - i])
return false ;
}
return true ;
}
public static void GeneratePalindromes( int n)
{
for ( int i = 1; i < n; i++)
{
if (IsPalindrome(i))
Console.Write(i + " " );
}
}
public static void Main( string [] args)
{
int n = 104;
GeneratePalindromes(n);
}
}
|
Javascript
function isPalindrome(n) {
const str = n.toString();
const len = str.length;
for (let i = 0; i < Math.floor(len / 2); i++) {
if (str[i] !== str[len - 1 - i])
return false ;
}
return true ;
}
function generatePalindromes(n) {
for (let i = 1; i < n; i++) {
if (isPalindrome(i))
process.stdout.write(i + " " );
}
}
const n = 104;
generatePalindromes(n);
|
Output
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101
Time Complexity: O(n * log10(n)) because we iterate over all numbers from 1 to n-1, which takes O(n) time, and for each number, we need to convert it into a string using to_string() function, which takes O(log10(n)) time, as the number of digits in a number is given by log10(n). Therefore, the overall time complexity is O(n * log10(n)).
Space Complexity: O(log10(n)) because we need to store the string representation of each number, which has a maximum length of log10(n). Therefore, the overall space complexity is O(log10(n)).
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Sep, 2023
Like Article
Save Article