In: Computer Science
Convert to binary 73
Algorithm:
Have a look at the below code. I have put comments wherever required for better understanding.
def dec_to_bin(n):
# create an array to store remainders
res = []
# run a loop while n is greater than 0
while (n>0):
# store the remainder
res.append(str(n%2))
# divide n by 2
n = n//2
# reverse the array
res.reverse()
# return the binary number
return "".join(res)
Happy Learning!