Advertisements
Advertisements
Question
Write a program to convert binary to decimal.
Code Writing
Advertisements
Solution
Process each binary digit from right to left and multiply it by its corresponding power of 2.
binary = input("Enter a binary number: ")
decimal = 0
power = 0
for digit in binary[::-1]:
decimal += int(digit) * (2 ** power)
power += 1
print("Decimal number =", decimal)
shaalaa.com
Is there an error in this question or solution?
