Binary to Decimal and Decimal to Binary Convertor in PYTHON November 03, 2021 Binary to Decimal Convertor Decimal to Binary Convertor in PYTHONimport math while True: decision = input("\n \nPress 1 for decimal to binary conversion\nPress 2 for binary to decimal conversion\n") if(decision=="1"): print("Instructions") print("1.) Only positive numbers") print("2.) Number should not be in fraction") print("3.) It should not contains alphabets or other special characters") n = input("Enter the number: ") n = int(n) mylist = [] if n <0: print("Please give numbers greator than 0") while True: m = n%2 if m == 1: quo = (n-1)/2 j = (n)%2 mylist.append(j) temp = n n = quo if quo == 1: mylist.append(1) break elif m == 0: quo = n/2 j = n%2 mylist.append(j) temp = n n = quo if quo == 1: mylist.append(1) break mylist.reverse() print("Your binary number is: ") for i in range(0, len(mylist)): mylist[i] = math.trunc(mylist[i]) print(mylist[i], end="") elif(decision=="2"): print("Instructions") print("1.) Binary should conatins only 0 and 1") print("2.) Should not be in fraction") print("3.) Should not be negative") s = input("Enter the binary number: ") s = int(s) a = 0 mylist3 = [int(x) for x in str(s)] mylist3.reverse() mylist2 = [] for i in range(0, len(mylist3)): powi = pow(2, i) mylist2.append(powi) a = a + (mylist3[i]*mylist2[i]) print("Your decimal number is: ") print(a) else: print("Please choose among 1 and 2.") John VeerBasic Python ProjectsContact mail id - john.veer.utube@gmail.comThanks for readingwww.basicpythonprogramme.blogspot.comwww.basicpythonprojects.blosgpot.com Share Get link Facebook X Pinterest Email Other Apps Share Get link Facebook X Pinterest Email Other Apps Comments
Comments
Post a Comment