0% found this document useful (0 votes)
17 views9 pages

Number Comversion

The document describes a Python program that allows users to convert between decimal, binary, octal, and hexadecimal number systems. It presents a menu with 13 options to perform conversions in either direction between the number systems. For each conversion option selected, it prompts the user to enter a number and then prints the equivalent value in the target number system.

Uploaded by

Raghu nath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Number Comversion

The document describes a Python program that allows users to convert between decimal, binary, octal, and hexadecimal number systems. It presents a menu with 13 options to perform conversions in either direction between the number systems. For each conversion option selected, it prompts the user to enter a number and then prints the equivalent value in the target number system.

Uploaded by

Raghu nath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Number conversion

print(" Number conversion ")


while True:
print()
print(" <!-----------------------------------!>")
print("1. Decimal to Binary")
print("2. Decimal to octal")
print("3. Decimal to Hexadecimal")
print("4. Binary to decimal")
print("5. Binary to octal")
print("6. Binary to Hexadecimal")
print("7. octal to decimal")
print("8. octal to Binary")
print("9. octal to Hexadecimal")
print("10.Hexadecimal to decimal")
print("11.Hexadecimal to Binary")
print("12.Hexadecimal to octal ")
print("13.Exit")
option = input("Enter your option: ")
if option == "1":

class dec_bin:
def DecToBin(self,d):
b=0
m=1
while d>0:
b = b+((d%2)*m)
m = m*10
d=int(d/2)
return b
print("Enter Decimal number: ",end="")
d_num=int(input())
ob = dec_bin()
b_num = ob.DecToBin(d_num)
print("\nEquivalent Binary value: ",b_num)

elif option == "2":


print("Enter Decimal Number: ", end = "")
d_num = int(input())
i=0
onum = []
while d_num!=0:
onum.insert(i, d_num%8)
i=i+1
d_num = int(d_num/8)
print("\nEquivalent octal value: ", end ="")
i=i-1
while i>=0:
print(onum[i], end="")
i=i-1
print()

elif option == "3":


print("Enter the decimal number: ", end ="")
dn = int(input())

i=0
hdn = []
while dn!=0:
rem = dn % 16
if rem<10:
rem = rem+48
else:
rem = rem+55
hdn.insert(i, chr(rem))
i=i+1
dn = int(dn/16)
print("\nEquivalent Hexadecimal value: ", end="")
i=i-1
while i>=0:
print(end=hdn[i])
i=i-1
print()

elif option == "4":


#binary to decimal number
print("Enter the binary number: ", end="")
#b_num=binary number
b_num = int(input())

#d_num=decimal number
d_num = 0
m=1
blen = len(str(b_num))

for i in range(blen):
rem = b_num%10
d_num=d_num + (rem * m)
m*=2
b_num = int(b_num/10)

print("\nEquivalent decimal value is: ",d_num)


elif option == "5":
print("Enter the Binary Number: ")
binarynum = int(input())

octaldigit = 0
octalnum = []
i=0
mul = 1
chk = 1
while binarynum!=0:
rem = binarynum % 10
octaldigit = octaldigit + (rem * mul)
if chk%3==0:
octalnum.insert(i, octaldigit)
mul = 1
octaldigit = 0
chk = 1
i = i+1
else:
mul = mul*2
chk = chk+1
binarynum = int(binarynum / 10)

if chk!=1:
octalnum.insert(i, octaldigit)

print("\nEquivalent Octal Value = ", end="")


i=0
while i>=0:
print(str(octalnum[i]), end="")
i = i-1
print()

elif option == "6":


print("Enter the Binary Number: ")
bnum = int(input())

hex = 0
mul = 1
chk = 1
i=0
hnum = []
while bnum!=0:
rem = bnum%10
hex = hex + (rem*mul)
if chk%4==0:
if hex<10:
hex = hex+48
val = chr(hex)
hnum.insert(i, val)
else:
hex = hex+55
val = chr(hex)
hnum.insert(i, val)
mul = 1
hex = 0
chk = 1
i = i+1
else:
mul = mul*2
chk = chk+1
bnum = int(bnum/10)

if chk!=1:
hex = hex+48
val = chr(hex)
hnum.insert(i, val)
if chk==1:
i = i-1

print("\nEquivalent Hexadecimal Value = ", end="")


while i>=0:
print(end=hnum[i])
i = i-1
print()

elif option == "7":


print("Enter the Octal Number: ")
octnum = int(input())

chk = 0
i=0
decnum = 0
while octnum!=0:
rem = octnum%10
if rem>7:
chk = 1
break
decnum = decnum + (rem * (8 ** i))
i = i+1
octnum = int(octnum/10)

if chk == 0:
print("\nEquivalent Decimal Value =", decnum)
else:
print("\nInvalid Input!")

elif option == "8":


#octal to binary number
print("Enter the octal Number: ")
octal=input()

binnum=""
octa=len(octal)
i=0
while i<octa:
if octal[i] == "0":
binnum=binnum + "000"

elif octal[i] == "1":


binnum = binnum + "001"
elif octal[i] == "2":
binnum = binnum + "010"
elif octal[i] == "3":
binnum = binnum + "011"
elif octal[i] == "4":
binnum = binnum + "100"
elif octal[i] == "5":
binnum = binnum + "101"
elif octal[i] == "6":
binnum = binnum + "110"
elif octal[i] == "7":
binnum = binnum + "111"

i=i+1

print("\nEquivalent binary value: ",binnum)

elif option == "9":


print("Enter the Octal Number: ")
octnum = int(input())

chk = 0
i=0
decnum = 0
while octnum!=0:
rem = octnum%10
if rem>7:
chk = 1
break
decnum = decnum + (rem * (8 ** i))
i = i+1
octnum = int(octnum/10)

if chk == 0:
i=0
hexdecnum = []
while decnum != 0:
rem = decnum % 16
if rem < 10:
rem = rem + 48
else:
rem = rem + 55
rem = chr(rem)
hexdecnum.insert(i, rem)
i=i+1
decnum = int(decnum / 16)

print("\nEquivalent Hexadecimal Value is: ")


i=i-1
while i >= 0:
print(end=hexdecnum[i])
i=i-1
print()

else:
print("\nInvalid Input!")

elif option == "10":


print("Enter the Hexadecimal Number: ")
hexdecnum = input()

chk = 0
decnum = 0
hexdecnumlen = len(hexdecnum)
hexdecnumlen = hexdecnumlen-1
i=0
while hexdecnumlen>=0:
rem = hexdecnum[hexdecnumlen]
if rem>='0' and rem<='9':
rem = int(rem)
elif rem>='A' and rem<='F':
rem = ord(rem)
rem = rem-55
elif rem>='a' and rem<='f':
rem = ord(rem)
rem = rem-87
else:
chk = 1
break
decnum = decnum + (rem * (16 ** i))
hexdecnumlen = hexdecnumlen-1
i = i+1

if chk == 0:
print("\nEquivalent Decimal Value: ", decnum)
else:
print("\nInvalid Input!")

elif option == "11":


#Hexadecimal to binary number
print("Enter the Hexadecimal Number: ")
hexdecnum = input()

binnum = ""
hexa = len(hexdecnum)
i=0
while i<hexa:
if hexdecnum[i] == '0':
binnum = binnum + "0000"

elif hexdecnum[i] == '1':


binnum = binnum + "0001"

elif hexdecnum[i] == '2':


binnum = binnum + "0010"

elif hexdecnum[i] == '3':


binnum = binnum + "0011"

elif hexdecnum[i] == '4':


binnum = binnum + "0100"

elif hexdecnum[i] == '5':


binnum = binnum + "0101"

elif hexdecnum[i] == '6':


binnum = binnum + "0110"

elif hexdecnum[i] == '7':


binnum = binnum + "0111"

elif hexdecnum[i] == '8':


binnum = binnum + "1000"

elif hexdecnum[i] == '9':


binnum = binnum + "1001"

elif hexdecnum[i] == 'a' or hexdecnum[i] == 'A':


binnum = binnum + "1010"

elif hexdecnum[i] == 'b' or hexdecnum[i] == 'B':


binnum = binnum + "1011"

elif hexdecnum[i] == 'c' or hexdecnum[i] == 'C':


binnum = binnum + "1100"

elif hexdecnum[i] == 'd' or hexdecnum[i] == 'D':


binnum = binnum + "1101"

elif hexdecnum[i] == 'e' or hexdecnum[i] == 'E':


binnum = binnum + "1110"

elif hexdecnum[i] == 'f' or hexdecnum[i] == 'F':


binnum = binnum + "1111"

i = i+1

print("\nEquivalent Binary Value: ", binnum)

elif option == "12":


print("Enter the Hexadecimal Number: ")
hexdecnum = input()

chk = 0
decnum = 0
hexdecnumlen = len(hexdecnum)
hexdecnumlen = hexdecnumlen-1
i=0
while hexdecnumlen>=0:
rem = hexdecnum[hexdecnumlen]
if rem>='0' and rem<='9':
rem = int(rem)
elif rem>='A' and rem<='F':
rem = ord(rem)
rem = rem-55
elif rem>='a' and rem<='f':
rem = ord(rem)
rem = rem-87
else:
chk = 1
break
decnum = decnum + (rem * (16 ** i))
hexdecnumlen = hexdecnumlen-1
i = i+1

if chk==0:
i=0
octnum = []
while decnum!=0:
rem = decnum%8
octnum.insert(i, rem)
i = i+1
decnum = int(decnum/8)

print("\nEquivalent Octal Value is: ")


i = i-1
while i>=0:
print(octnum[i], end="")
i = i-1
print()
else:
print("\nInvalid Input!")

elif option == "13":


break

You might also like