English

Write a program to convert decimal number to binary.

Advertisements
Advertisements

Question

Write a program to convert decimal number to binary.

Code Writing
Advertisements

Solution

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
  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 14. | Page 27
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×