(Binary to decimal) Write a recursive function that parses a binary number as a string into a decimal integer. The function header is as follows:
def binaryToDecimal(binaryString):
Write a test program that prompts the user to enter a binary string and displays its decimal equivalent.
Binary to decimal Program code:
#Recursive function to convert a binary number to decimal.
def binaryToDecimal(binaryString):
# Base case: If the binary string is empty, return 0.
if binaryString == '':
return 0
# Recursive case: Convert the binary number
#excluding the last digit to decimal,
# multiply it by 2, and add the value of the last digit.
return binaryToDecimal(binaryString[:-1]) * 2 + int(binaryString[-1])
# Prompt the user for input
binary = input("Enter a binary number: ")
# Convert the binary number to decimal
decimal = binaryToDecimal(binary)
# Display the result
print(f"The decimal equivalent of {binary} is: {decimal}")
Executed Output:
Enter a binary number: 1101
The decimal equivalent of 1101 is: 13