हिंदी

Write a program to convert decimal number to binary.

Advertisements
Advertisements

प्रश्न

Write a program to convert decimal number to binary.

कोड लेखन
Advertisements

उत्तर

Repeatedly divide the decimal number by 2 and store each remainder in reverse order.

n = int(input("Enter a decimal number: "))
binary = ""

if n == 0:
    binary = "0"
else:
    while n > 0:
        binary = str(n % 2) + binary
        n //= 2

print("Binary number =", binary)
 
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 1: Review of Phython - EXERCISE [पृष्ठ २७]

APPEARS IN

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

Englishहिंदीमराठी


      Forgot password?
Use app×