Program to Print All Co-binary Palindromic Numbers in a Range

Python Program to Print All Co-binary Palindromic Numbers in a Range

Given the lower limit range and upper limit range, the task is to print all Co-binary Palindromic Numbers in the given range in Python.

Co-binary Palindromic Numbers:

A co-binary palindrome is a number that is a palindrome both as a decimal number and after being binary transformed.

Examples:

Example1:

Input:

Given upper limit range =11
Given lower limit range =2426

Output:

The Co-binary palindrome numbers in the given range 11 and 2426 are:
33 99 313 585 717

Example2:

Input:

Given upper limit range =5
Given lower limit range =12564

Output:

The Co-binary palindrome numbers in the given range 5 and 12564 are:
5 7 9 33 99 313 585 717 7447 9009

Program to Print All Co-binary Palindromic Numbers in a Range in

Python

Below are the ways to print all Co-binary Palindromic Numbers in the given range in Python.

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the lower limit range as static input and store it in a variable.
  • Give the upper limit range as static input and store it in another variable.
  • Create a function checkpalindromicNumb() which accepts the string as an argument and returns true if the string is palindrome else it returns False.
  • Create a function convertBinar() which converts the given number to binary and returns it.
  • Loop from lower limit range to upper limit range using For loop.
  • Convert this iterator value to binary by passing it as an argument to convertBinar() function and store it in a variable say binarystrng.
  • Convert this iterator value to a string using the str() function say strngnumb.
  • Check if the strngnumb is palindrome or not by giving the given strngnumb as an argument to checkpalindromicNumb().
  • Check if the binarystrng is palindrome or not by giving the given binarystrng as an argument to checkpalindromicNumb().
  • Check if both statements are true using the and operator and If conditional Statement.
  • If it is true then print it.
  • The Exit of the Program.

Below is the implementation:

# Create a function checkpalindromicNumb() which accepts the string as an argument and
# returns true if the string is palindrome else it returns False.
def checkpalindromicNumb(val):
    return val == val[::-1]
# Create a function convertBinar() which converts the given number to binary and returns it.
def convertBinar(orinumb):
    return bin(orinumb)[2:]
# Give the lower limit range as static input and store it in a variable.
lowlimrange = 11
# Give the upper limit range as static input and store it in another variable.
upplimrange = 2426
print('The Co-binary palindrome numbers in the given range',
      lowlimrange, 'and', upplimrange, 'are:')
# Loop from lower limit range to upper limit range using For loop.
for itervalu in range(lowlimrange, upplimrange+1):
    # Convert this iterator value to binary by passing it as an argument
    # to convertBinar() function and store it in a variable say binarystrng.
    binarystrng = convertBinar(itervalu)
    # Convert this iterator value to a string
    # using the str() function say strngnumb.
    strngnumb = str(itervalu)
    # Check if the strngnumb is palindrome or not by giving the given strngnumb
    # as an argument to checkpalindromicNumb().
    # Check if the binarystrng is palindrome or not by giving the given binarystrng
    # as an argument to checkpalindromicNumb().
    # Check if both statements are true using the and operator and If conditional Statement.
    if(checkpalindromicNumb(binarystrng) and checkpalindromicNumb(strngnumb)):
        # If it is true then print it.
        print(strngnumb, end=' ')

Output:

The Co-binary palindrome numbers in the given range 11 and 2426 are:
33 99 313 585 717

Method #2: Using For Loop (User Input)

Approach:

  • Give the lower limit range and upper limit range as user input using map(),int(),split() functions.
  • Store them in two separate variables.
  • Create a function checkpalindromicNumb() which accepts the string as an argument and returns true if the string is palindrome else it returns False.
  • Create a function convertBinar() which converts the given number to binary and returns it.
  • Loop from lower limit range to upper limit range using For loop.
  • Convert this iterator value to binary by passing it as an argument to convertBinar() function and store it in a variable say binarystrng.
  • Convert this iterator value to a string using the str() function say strngnumb.
  • Check if the strngnumb is palindrome or not by giving the given strngnumb as an argument to checkpalindromicNumb().
  • Check if the binarystrng is palindrome or not by giving the given binarystrng as an argument to checkpalindromicNumb().
  • Check if both statements are true using the and operator and If conditional Statement.
  • If it is true then print it.
  • The Exit of the Program.

Below is the implementation:

# Create a function checkpalindromicNumb() which accepts the string as an argument and
# returns true if the string is palindrome else it returns False.
def checkpalindromicNumb(val):
    return val == val[::-1]
# Create a function convertBinar() which converts the given number to binary and returns it.
def convertBinar(orinumb):
    return bin(orinumb)[2:]

# Give the lower limit range and upper limit range as
# user input using map(),int(),split() functions.
# Store them in two separate variables.
lowlimrange, upplimrange = map(int, input(
    'Enter lower limit range and upper limit range separate bt spaces = ').split())
print('The Co-binary palindrome numbers in the given range',
      lowlimrange, 'and', upplimrange, 'are:')
# Loop from lower limit range to upper limit range using For loop.
for itervalu in range(lowlimrange, upplimrange+1):
    # Convert this iterator value to binary by passing it as an argument
    # to convertBinar() function and store it in a variable say binarystrng.
    binarystrng = convertBinar(itervalu)
    # Convert this iterator value to a string
    # using the str() function say strngnumb.
    strngnumb = str(itervalu)
    # Check if the strngnumb is palindrome or not by giving the given strngnumb
    # as an argument to checkpalindromicNumb().
    # Check if the binarystrng is palindrome or not by giving the given binarystrng
    # as an argument to checkpalindromicNumb().
    # Check if both statements are true using the and operator and If conditional Statement.
    if(checkpalindromicNumb(binarystrng) and checkpalindromicNumb(strngnumb)):
        # If it is true then print it.
        print(strngnumb, end=' ')

Output:

Enter lower limit range and upper limit range separate bt spaces = 5 12564
The Co-binary palindrome numbers in the given range 5 and 12564 are:
5 7 9 33 99 313 585 717 7447 9009

Related Programs: