हिंदी

Write a program to convert binary to decimal.

Advertisements
Advertisements

प्रश्न

Write a program to convert binary to decimal.

कोड लेखन
Advertisements

उत्तर

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 1: Review of Phython - EXERCISE [पृष्ठ २७]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
अध्याय 1 Review of Phython
EXERCISE | Q 15. | पृष्ठ २७
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×