# C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.

Find all numbers less than n, which are palindromic in base 10 as well as base 2.

Examples:

33 is Palindrome in its decimal representation. 100001(binary equivalent of 33) in Binary is a Palindrome. 313 is Palindrome in its decimal representation. 100111001 (binary equivalent of 313) in Binary is a Palindrome.

**Brute Force: **

We check all the numbers from 1 to n whether its decimal representation is palindrome or not.

Further, if a number is palindromic in base 10 then we check for its binary representation. If we found its both representations a palindrome then we print it.

**Efficient Approach: **

We start from 1 and create palindromes of odd digit and even digit up to n and check whether its binary representation is palindrome or not.

Note: This will reduce the number of operations as we should check only for decimal palindrome instead of checking all numbers from 1 to n.

This approach uses two methods:

** int createPalindrome(int input, int b, bool isOdd): **

The palindrome creator takes in an input number and a base b as well as a boolean telling if the palindrome should have an even or odd number of digits. It takes the input number, reverses it and appends it to the input number. If the result should have an odd number of digits, it chops off a digit of the reversed part.

**bool IsPalindrome(int number, int b)**

It takes the input number, calculate its reverse according to base b. Return result whether number is equal to its reverse or not.

`// A C++ program for finding numbers which are ` `// decimal as well as binary palindrome ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// A utility to check if number is palindrome on base b ` `bool` `IsPalindrome(` `int` `number, ` `int` `b) ` `{ ` ` ` `int` `reversed = 0; ` ` ` `int` `k = number; ` ` ` ` ` `// calculate reverse of number ` ` ` `while` `(k > 0) { ` ` ` `reversed = b * reversed + k % b; ` ` ` `k /= b; ` ` ` `} ` ` ` ` ` `// return true/false depending upon number is palindrome or not ` ` ` `return` `(number == reversed); ` `} ` ` ` `// 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; ` `} ` ` ` `// Funtion to print decimal and binary palindromic number ` `void` `findPalindromic(` `int` `n) ` `{ ` ` ` `int` `number; ` ` ` `for` `(` `int` `j = 0; j < 2; j++) { ` ` ` `bool` `isOdd = (j % 2 == 0); ` ` ` ` ` `// Creates palindrome of base 10 upto n ` ` ` `// j always decides digits of created palindrome ` ` ` `int` `i = 1; ` ` ` `while` `((number = createPalindrome(i, 10, isOdd)) < n) { ` ` ` `// if created palindrome of base 10 is ` ` ` `// binary palindrome ` ` ` `if` `(IsPalindrome(number, 2)) ` ` ` `cout << number << ` `" "` `; ` ` ` `i++; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Program to test above function ` `int` `main() ` `{ ` ` ` `int` `n = 1000; ` ` ` `findPalindromic(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 3 5 7 9 313 585 717 33 99

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.

## Recommended Posts:

- Given a number N in decimal base, find number of its digits in any base (base b)
- Write a program to add two numbers in base 14
- All possible numbers of N digits and base B without leading zeros
- Count of N-digit numbers in base K with no two consecutive zeroes
- Find the height of a right-angled triangle whose area is X times its base
- Pandigital number in a given base
- Double Base Palindrome
- Complement of a number with any base b
- Check whether a number has consecutive 0's in the given base or not
- Check if a number N starts with 1 in b-base
- Check if a given number can be represented in given a no. of digits in any base
- Check if the number is even or odd whose digits and base (radix) is given
- Convert from any base to decimal and vice versa
- Number of trailing zeroes in base B representation of N!
- Check if a number is power of k using base changing method