(Decimal to binary) Write a recursive function that converts a decimal number into a binary number as a string. The function header is as follows:
def decimalToBinary(value):
Write a test program that prompts the user to enter a decimal number and displays its binary equivalent.
Decimal to binary Program code:
#Recursive function 'decimalToBinary' accepts an argument value,
#to convert a decimal number to binary.
def decimalToBinary(value):
# Base case: If the value is 0, return '0'.
if value == 0:
return '0'
# Recursive case: Convert the quotient of value divided by 2 to binary,
# concatenate it with the remainder of value divided by 2, and return the result.
return decimalToBinary(value // 2) + str(value % 2)
# Prompt the user for input
decimal = int(input("Enter a decimal number: "))
# Convert the decimal number to binary
binary = decimalToBinary(decimal)
# Display the result
print(f"The binary equivalent of {decimal} is: {binary}")
Executed Output:
Enter a decimal number: 55
The binary equivalent of 55 is: 0110111