Last active
September 25, 2020 12:32
-
-
Save techb/3ab9bd3b202b22e078a7ba9def693708 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def decimal2binary(n): | |
# check if larger than 255 as per requirements | |
if n > 255: | |
return "Int too large" | |
# this is the string we're returning | |
binStr = "" | |
# this will loop forever if n never gets to or below 0 | |
while n > 0: | |
# check modulo of n | |
if n%2 == 0: | |
binStr = "0" + binStr | |
else: | |
binStr = "1" + binStr | |
# set n for next iteration, this is our exit condition | |
n = n/2 | |
# return the bin string we built | |
return binStr | |
# lets test it out | |
x = input("What int do you want to convert? ") | |
output = decimal2binary(x) | |
print( output ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment