Binary >> << Decimal

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Binary >> << Decimal

Post 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
User avatar
Arrexel
New User
New User
 
Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Location: Ontario, Canada
Blog: View Blog (0)


Re: Binary >> << Decimal

Post by YayPython on Tue Feb 17, 2009 3:33 pm
([msg=18120]see Re: Binary >> << Decimal[/msg])

This is cool! I might use this for math class...thanks. I will download when I get a chance.
User avatar
YayPython
Experienced User
Experienced User
 
Posts: 50
Joined: Sat Jan 31, 2009 11:05 am
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests

cron