In: Computer Science
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints the decimal value of the binary number represented by the entered bits, i.e. 25 in this case.
Short Summary:
Provided the source code and sample output as per the requirements.
Source Code:
# function that calculates and prints the decimal value of the
binary number
def binaryToDecimal(binaryNumber):
decimalNumber = 0
index = 0
# continues until the binaryNumber not equal to 0
while(binaryNumber != 0):
# getting the remainder of
decimal = binaryNumber % 10
# finding the decimal number by finding out the power
decimalNumber = decimalNumber + decimal * pow(2, index)
# // rounds the result down to the nearest whole number
binaryNumber = binaryNumber//10
# increment the index value
index += 1
return decimalNumber
# calling the conversion function
bToD = binaryToDecimal(11001)
# printing the result
print(bToD)
Refer the following screenshots for code indentation:
Sample Run:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************