Python Program to Convert Decimal Number to Binary String using Recursion

In the previous article, we have discussed Python Program to Convert Binary to Decimal using Recursion
Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Binary Code:

As previously stated, Binary Code is a Base-2 representation of a number. In Binary, all numbers are represented by simply two symbols: 0 and 1. Binary (also known as base-2) is a numerical system with only two digits: 0 and 1.

Given a binary string, the task is to convert the given decimal number to a binary string using recursion in Python.

Examples:

Example1:

Input:

Given binary number = 153

Output:

The decimal Equivalent of the given binary number { 153 } is : 10011001

Example2:

Input:

Given binary number = 408

Output:

The decimal Equivalent of the given binary number { 408 } is : 110011000

Program to Convert Decimal Number to Binary String using Recursion in Python

Below are the ways to convert the given decimal number to a binary string using recursion in Python:

Method #1: Using Recursion (Static Input)

Approach:

  • Give the decimal number as static input and store it in a variable.
  • Create a recursive function to say deciToBinary() which accepts the decimal number as an argument and returns the binary equivalent of the given decimal number.
  • Inside the deciToBinary() function, Check if the decimal number is equal to 0 using the if conditional statement.
  • If it is true then return 0.
  • Else return (n% 2 + 10* deciToBinary(n // 2)) {Recursive logic}
  • Inside the main function pass the given decimal number as an argument to deciToBinary() which returns the
  • binary equivalent of the given decimal number.
  • Print the binary equivalent of the given decimal number.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say deciToBinary() which accepts
# the decimal number as an argument and returns the binary equivalent
# of the given decimal number.


def deciToBinary(decinumb):
    # Inside the deciToBinary() function,
    # Check if the decimal number is equal to 0 using the if conditional statement.
    if(decinumb == 0):
        # If it is true then return 0.
        return 0
    # Else return (n% 2 + 10* deciToBinary(n // 2)) {Recursive logic}
    return (decinumb % 2 + 10 * deciToBinary(decinumb // 2))


# Give the decimal number as static input and store it in a variable.
gvndecimalnumb = 153
# Inside the main function pass the given decimal number as an argument
# to deciToBinary() which returns the
# binary equivalent of the given decimal number.
resbinarynumbr = deciToBinary(gvndecimalnumb)
# Print the decimal equivalent of the given binary number.
print(
    'The decimal Equivalent of the given binary number {', gvndecimalnumb, '} is :', resbinarynumbr)

Output:

The decimal Equivalent of the given binary number { 153 } is : 10011001

Method #2: Using Recursion (User Input)

Approach:

  • Give the binary number as user input using the int(input()) function and store it in a variable.
  • Create a recursive function to say deciToBinary() which accepts the decimal number as an argument and returns the binary equivalent of the given decimal number.
  • Inside the deciToBinary() function, Check if the decimal number is equal to 0 using the if conditional statement.
  • If it is true then return 0.
  • Else return (n% 2 + 10* deciToBinary(n // 2)) {Recursive logic}
  • Inside the main function pass the given decimal number as an argument to deciToBinary() which returns the binary equivalent of the given decimal number.
  • Print the binary equivalent of the given decimal number.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say deciToBinary() which accepts
# the decimal number as an argument and returns the binary equivalent
# of the given decimal number.


def deciToBinary(decinumb):
    # Inside the deciToBinary() function,
    # Check if the decimal number is equal to 0 using the if conditional statement.
    if(decinumb == 0):
        # If it is true then return 0.
        return 0
    # Else return (n% 2 + 10* deciToBinary(n // 2)) {Recursive logic}
    return (decinumb % 2 + 10 * deciToBinary(decinumb // 2))


# Give the binary number as user input using the int(input()) function and store it in a variable.
gvndecimalnumb = int(input('Enter some random decimal number = '))
# Inside the main function pass the given decimal number as an argument
# to deciToBinary() which returns the
# binary equivalent of the given decimal number.
resbinarynumbr = deciToBinary(gvndecimalnumb)
# Print the decimal equivalent of the given binary number.
print(
    'The decimal Equivalent of the given binary number {', gvndecimalnumb, '} is :', resbinarynumbr)

Output:

Enter some random decimal number = 408
The decimal Equivalent of the given binary number { 408 } is : 110011000

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.