English

Write a program to convert binary to decimal.

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?
Chapter 1: Review of Phython - EXERCISE [Page 27]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 1 Review of Phython
EXERCISE | Q 15. | Page 27
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×