In the previous article, we have discussed Python Program for Sign Change
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 binary string to a decimal number using recursion in Python.
Examples:
Example1:
Input:
Given binary number = 1011
Output:
The decimal Equivalent of the given binary number { 1011 } is : 11Example2:
Input:
Given binary number = 10010101
Output:
The decimal Equivalent of the given binary number { 10010101 } is : 149Program to Convert Binary to Decimal using Recursion in Python
Below are the ways to convert the given binary string to a decimal number using recursion in Python:
Method #1: Using Recursion (Static Input)
Approach:
- Give the binary number as static input and store it in a variable.
- Create a recursive function to say binaryToDeci() which accepts the binary number as an argument and returns the decimal equivalent of the given binary string.
- Inside the binaryToDeci()Â function, Check if the binary number is equal to 0 using the if conditional statement.
- If it is true then return 0.
- Else return (n% 10 + 2* binaryToDeci(n // 10)) {Recursive logic where n%10 gives rightmostbit and n//10 divides the number by 10 which removes the last bit}.
- Inside the main function pass the given binary number as an argument to binaryToDeci()Â which returns the decimal equivalent of the given binary number.
- Print the decimal equivalent of the given binary number.
- The Exit of the Program.
Below is the implementation:
# Create a recursive function to say binaryToDeci()
# which accepts the binary number as an argument
# and returns the decimal equivalent of the given binary string.
def binaryToDeci(binanumb):
# Inside the binaryToDeci()Â function,
# Check if the binary number is equal to 0 using the if conditional statement.
if(binanumb == 0):
# If it is true then return 0.
return 0
# Else return (n% 10 + 2* binaryToDeci(n // 10))
# {Recursive logic where n%10 gives rightmostbit and n//10 divides the number by 10}.
return (binanumb % 10 + 2 * binaryToDeci(binanumb // 10))
# Give the binary number as static input and store it in a variable.
gvnbinarynumb = 1011
# Inside the main function pass the given binary number as an argument
# to binaryToDeci()Â which returns the decimal equivalent of the given binary number.
resdecimalnumbr = binaryToDeci(gvnbinarynumb)
# Print the decimal equivalent of the given binary number.
print(
'The decimal Equivalent of the given binary number {', gvnbinarynumb, '} is :', resdecimalnumbr)
Output:
The decimal Equivalent of the given binary number { 1011 } is : 11Method #2: Using Recursion (User Input)
Approach:
- Give the binary number as user input using int(input()) function and store it in a variable.
- Create a recursive function to say binaryToDeci() which accepts the binary number as an argument and returns the decimal equivalent of the given binary string.
- Inside the binaryToDeci()Â function, Check if the binary number is equal to 0 using the if conditional statement.
- If it is true then return 0.
- Else return (n% 10 + 2* binaryToDeci(n // 10)) {Recursive logic where n%10 gives rightmostbit and n//10 divides the number by 10 which removes the last bit}.
- Inside the main function pass the given binary number as an argument to binaryToDeci()Â which returns the decimal equivalent of the given binary number.
- Print the decimal equivalent of the given binary number.
- The Exit of the Program.
Below is the implementation:
# Create a recursive function to say binaryToDeci()
# which accepts the binary number as an argument
# and returns the decimal equivalent of the given binary string.
def binaryToDeci(binanumb):
# Inside the binaryToDeci()Â function,
# Check if the binary number is equal to 0 using the if conditional statement.
if(binanumb == 0):
# If it is true then return 0.
return 0
# Else return (n% 10 + 2* binaryToDeci(n // 10))
# {Recursive logic where n%10 gives rightmostbit and n//10 divides the number by 10}.
return (binanumb % 10 + 2 * binaryToDeci(binanumb // 10))
# Give the binary number as user input using int(input()) function
# and store it in a variable.
gvnbinarynumb = int(input('Enter some random binary number = '))
# Inside the main function pass the given binary number as an argument
# to binaryToDeci()Â which returns the decimal equivalent of the given binary number.
resdecimalnumbr = binaryToDeci(gvnbinarynumb)
# Print the decimal equivalent of the given binary number.
print(
'The decimal Equivalent of the given binary number {', gvnbinarynumb, '} is :', resdecimalnumbr)
Output:
Enter some random binary number = 10010101
The decimal Equivalent of the given binary number { 10010101 } is : 149Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
