''' Program : Number System Conversion '''
def bin_deci(num) : # for e.g. if num = 0101
power = 0 # 0 1 2
total = 0 # 0 1 1
while num > 0: # true 10>0 1>0
flr_dvsn = num // 10 # 10 1 0
ex = (num % 10) * (2 ** power) # ex = 1*2^0=1 ex=0*2^1=0 ex=1*4=4
power += 1 # 1 2 3
num = flr_dvsn # 10 1 0
total += ex # 1 1 5
return(total)
def oct_dec(num):
power = 0
total = 0
lst = list(str((num)))
for i in range(len(lst)):
if lst[i] == '8' or lst[i] == '9':
print("Please Enter Octal Number i.e. 0-7")
break
else:
while num > 0:
f = num // 10
ex = (num % 10) * (8 ** power)
power += 1
num = f
total += ex
return(total)
ns = input("Enter Number System Which you want to convert : ")
cv = input("Enter number system in Which you want to convert : ")
numsys = ns.capitalize()
conversion = cv.capitalize()
if numsys == 'Binary':
num = int(input("Enter Binary Number :",))
if conversion == "Decimal" :
print("Decimal Conversion of", num, "is", bin_deci(num))
elif conversion == "Octal":
oct_deci = bin_deci(num)
print("Octal of", num, "is", oct(oct_deci))
elif conversion == "Hexadecimal" or "Hexa Decimal" or "HexaDecimal":
hexa_deci = bin_deci(num)
print("Hexa_Decimal of", num, "is :", hex(hexa_deci))
else:
print("Please Enter Binary Number!")
elif numsys == "Decimal":
num = int(input("Enter Decimal Number :", ))
if conversion == "Binary":
print("Binary of", num, "is :", bin(num))
elif conversion == "Octal":
print("Octal of", num, "is :", oct(num))
elif conversion == "Hexadecimal" or "Hexa Decimal" or "HexaDecimal":
print("HexaDecimal of", num, "is :", hex(num))
else:
print("Please Enter valid Decimal Number!")
elif numsys == 'Octal':
num = int(input("Enter Octal Number : "))
if conversion == "Decimal":
print("Decimal Conversion of", num ,"is :", oct_dec(num))
elif conversion == "Binary":
oct_bin = oct_dec(num)
print("Converting Octal(", num, ") to Binary :", bin(oct_bin))
elif conversion == "Hexadecimal" or "Hexa Decimal" or "Hexadecimal":
oct_hex = oct_dec(num)
print("Converting Octal)", num, ") to Hexa_Decimal :", hex(oct_hex))
else:
print("Please Enter Octal Value!")
else:
print("Sorry! we are not able to convert Hexadecimal to any other Number Conversion!")