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?
