by Arrexel on Mon Feb 16, 2009 10:57 am
([msg=18019]see Binary >> << Decimal[/msg])
I've been bored lately, writing some codes that may be useful at times(e.g., factorial, permutation calc, etc.). Here is a simple program that converts from either binary to decimal or decimal to binary.
- Code: Select all
print "1) Decimal -> Binary\n2) Binary -> Decimal"
usersel = int(raw_input("Please enter your selection: "))
if usersel == 1:
finbin = ""
numbin = float(raw_input("Enter the base 10 number: "))
numbinb = numbin
while numbin > 1:
if numbin % 2 == 0:
finbin = "0" + finbin
numbin = numbin / 2
else:
finbin = "1" + finbin
numbin = numbin / 2 - 0.5
finbin = "1" + finbin
print int(numbinb),"in binary is",finbin
elif usersel == 2:
binnum = raw_input("Enter the base 2 number: ")
findec = int((binnum),2)
print binnum,"in decimal is",findec
"I think there is a world market for maybe five computers."
Unverified Quote, 1945 - Thomas J. Watson, Founder of IBM