This post contains a total of 29+ Python Decimal to Binary Converter examples with Source Code. All these Decimal to Binary converters are made using Python Programming language.
You can use the source code of these examples with credits to the original owner.
Related Posts
Python Decimal to Binary Converter Examples
1. By Mitali
Made by Mitali. For a given decimal number as input,this code will convert the decimal number into equivalent binary number, E.g If input is “25” the output will be “11001”. Source
The binary equivalent of 1 is 1 .
number=int(input())
temp=number
binary=""
while number>0:
rem=number%2
binary+=str(rem)
number=number//2
print("The binary equivalent of "+str(temp)+" is "+binary[::-1]+" .")
print("Thank you for trying this code.")
print("Hit like if you like it.")
2. By Ona Nixon
Made by Ona Nixon. Basic Python Program to convert decimal number to binary. Source
2 10
def conveter(num):
if num > 1:
conveter (num // 2)
print(num % 2, end='')
decimal=int(input())
conveter(decimal)
3. By Samarth
Made by Samarth. Enter any number to see its binary form. Source
Binary Form Of 3 is 11
n = int(input()) #Your number.
bin = "" #Empty string to hold the binary form of the number.
temp = n
#Set of functions to get result =>
while n>0:
rem = n % 2
bin+=str(rem) #Binary form of the entered number is reverse of what we get here. I got help from codes on the net.
n = n // 2
print("Binary Form Of",temp,"is",bin[::-1],"\n")
print("Thanks For Viewing My Code! ^^")
4. By Sololearn
Made by Sololearn. Source
42 101010
get_bin = lambda x: format(x, 'b')
sample_num = 42
print(get_bin(sample_num))
5. By Shuaib Nuruddin
Made by Shuaib Nuruddin. Source
5 in binary is 101
integer = input()
a = int(integer)
b = 2
List = []
while a/b > 0:
List.append(a%b)
a//=b
List.reverse()
print(integer," in binary is ",*List,sep=(""))
6. By Justine Ogaraku
Made by Justine Ogaraku. Source
6 110
def binary(number,d=''):
if number<2:
d+=str(1)
print(d[::-1])
else:
d+=str(number%2)
binary(number//2,d=d)
binary(int(input()))
7. By Chandrajeet Varma
Made by Chandrajeet Varma. Source
Enter Decimal number : 7 Binary number is = 111
n=int(input("Enter Decimal number : "));print(n);a=""
while(n>0): b=str(n%2); n//=2; a+=b
print("Binary number is =",a[::-1])
8. By Ebubekir Türker
Made by Ebubekir Türker. Source
8 is equal to 1000 in binary.
"""
PLEASE INPUT INTEGER VALUE
"""
def dectobin(y):
if y==0:
return "0"
if y==1:
return "1"
h=""
while y>1:
x=int(y/2)
rmdnr=y%2#remainder
y=int(y/2)
h+=str(rmdnr)
if x>1:
continue
else:
h+=str(x)
h=h[::-1]
return h
i=int(input())
print (str(i)+" is equal to "+dectobin(i)+" in binary.")
9. By Abdessamad
Made by Abdessamad. Source
9 Your binary is: 1001
def dec_to_binary(n):
bits = []
while n > 0:
bits.append(n%2)
n = n // 2
bits.reverse()
binary = ''
for bit in bits:
binary += str(bit)
return binary
num = int(input(" "))
binary = dec_to_binary(num)
print("Your binary is:", binary)
10. By Nitin Rawat
Made by Nitin Rawat. Python Decimal to Binary converter without using bin(),dec(),oct(),hex() functions. Source
enter any decimal number:10 Equivalent Binary number:1010
n = int(input("enter any decimal number:"))
print(n)
a=1
list=[]
while(a!=0):
list.append(n%2)
n=n//2
a=n
print("Equivalent Binary number:",end="")
for i in range((len(list)-1),-1,-1):
print(list[i],end="")
11. By Vismay Katharani
Made by Vismay Katharani. Source
Enter a Decimal Number:11 0b1011 in binary. 0o13 in octal. 0xb in hexadecimal.
dec = int(input("Enter a Decimal Number:\n"))
print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")
12. By Akash
Made by Akash. Source
Enter any number of base 10 : In Decimal : 12 In Binary : 1100 In Octal : 14 In Hexadecimal : C
def dec_convert(s_sys,system,num):
bin = []
while num>=system:
temp = num%system
if temp>9:
temp = chr(65+(temp-10))
num //= system
bin.append(temp)
if num>9:
num = chr(65+(num-10))
bin.append(num)
print('In '+s_sys,end='')
res = ''
for x in list(reversed(bin)):
res += str(x)
print(res)
num = int(input("Enter any number of base 10 : \n"))
print("In Decimal : "+str(num))
dec_convert("Binary : ",2,num)
dec_convert("Octal : ",8,num)
dec_convert ("Hexadecimal : ",16,num)
13. By Med Arezki
Made by Med Arezki. A single line python code for decimal to binary conversion. Source
13 0b1101
print(bin(int(input())))
14. By Agnibha Chakraborty
Made by Agnibha Chakraborty. Decimal to Binary program using Recursive. Source
0 --> 0 1 --> 1 2 --> 10 3 --> 11 4 --> 100 5 --> 101 6 --> 110 7 --> 111 8 --> 1000 9 --> 1001 10 --> 1010
def dec_to_bin(n):
return n if n <2 else str(dec_to_bin(n//2)) + str(n % 2)
for i in range(101):
print(i,"-->",dec_to_bin(i))
15. By ƒred
Made by ƒred. Source
15 0b1111
'''
Code: Decimal To Binary.
Desc: Converts given base 10
integer to it's binary representation.
Version: 0.0.1v
Written by: Fred durian.
'''
def to_bin(n, pfx="0b"):
_=''
if not n:return pfx+'0'
elif abs(n)!=n:n=1+~n;pfx='-%s'%pfx
while n:_+=(n&1).__str__();n>>=1
return pfx+_[::-1]
print (to_bin(int(input())))
16. By Edilson Zau
Made by Edilson Zau. Enter a Decimal number as your input., the number should be an integer and the program will output binary. Source
You entered: 16 ======================== Your input in Binary is: 10000 ========================
def convert_to_binary(n):
if n > 1:
convert_to_binary(n//2)
print (n % 2, end="")
decimal = int(input())
print ("You entered: \n" + str(decimal))
print (" ")
print ("=="*12, "\n")
print ("Your input in Binary is:")
convert_to_binary(decimal)
print ("\n")
print ( "=="*12)
17. By Lino Etis Asmolo Jr
Made by Lino Etis Asmolo Jr. Source
Input a decimal number: 17 Decimal: 17 is 10001 in Binary
def dec_to_bin(num):
still = True
bases=[]
index=0
while still:
bases.append(2**index)
if bases[index] > num:
still = False
del bases[index]
index += 1
temp = bases[len(bases)-1]
bin = '1'
for i in range(len(bases)-2,-1,-1):
if bases[i] + temp <= num:
temp += bases[i]
bin += '1'
else:
bin += '0'
print(f'Decimal: {num} is {bin} in Binary')
num = int(input('Input a decimal number: '))
print('')
dec_to_bin(num)
18. By Code Ninja
Made by Code Ninja. Source
binary conversion of 18 is 10010
print("welcome to the decimal to binary converter ")
Input = input()
try:
decNum = int(Input)
except ValueError :
print ("\n" +"'" +Input+ "'"+ "\nis a invalid decimal number.\nPlease enter a valid number.")
def converter(decNum):
emptyString = ""
binNum = emptyString + str(decNum%2)
given_decNum = decNum
while decNum//2 >= 1 :
decNum = decNum//2
binNum = binNum + str(decNum%2)
print("\nbinary conversion of " + str(given_decNum) + " is " + binNum[::-1])
try:
converter( decNum )
except NameError :
print("")
print ("\nthank you for visiting!")
19. By Jerico Bote
Made by Jerico Bote. Enter a decimal integer to convert to binary. Source
enter an integer:19 binary form of decimal number 19 is: 1 0 0 1 1
x=input("enter an integer:\n")
try:
assert float(x)==int(x)
x=abs(int(x))
except (ValueError , AssertionError):
print("invalid input, please try again")
x=None
if x!=None:
z=0
w=0
y=x
while x - 2**(z+1) >= 0:
z+=1
w=2**(z)
result=[]
while True:
if z<0:
print("binary form of decimal number {0} is:".format(y))
print(*result)
break
if x-w>=0 and x!=0:
x-=w
result.append("1")
else:
result.append("0")
z-=1
w=2**z
20. By MD. Ferdous Ibne Abu Bakar
Made by MD. Ferdous Ibne Abu Bakar. Source
Decimal: 20 Binary: 10100
try:
decimal = int(input())
convert = bin(decimal)
binary = convert.replace('0b','') #This line is extra. (I used this for remove the 'Ob' from the output)
print ('Decimal: '+str(decimal))
print ('Binary: '+str(binary))
print ('''
About Binary number:
A binary number is a number expressed in the base-2 numeral system or binary numeral system, a method of mathematical expression which uses only two symbols: typically "0" (zero) and "1" (one).
The base-2 numeral system is a positional notation with a radix of 2. Each digit is referred to as a bit, or binary digit. Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used by almost all modern computers and computer-based devices, as a preferred system of use, over various other human techniques of communication, because of the simplicity of the language.
-wikipedia
''')
except ValueError:
print ('Please input only a integer number')
21. By just trying to think
Made by just trying to think. Source
21 10101
# Decimal to Binary
u = int(input())
s = ''
while u >= 1:
if u % 2 == 0:
s = s + '0'
elif u % 2 == 1:
s = s + '1'
elif u == 1:
s = s + '1'
u = u // 2
print(s[::-1])
22. By NewComer
Made by NewComer. Source
22 10110
n=int(input())
a=""
while(n>0):
a += str(n%2)
n //= 2
print (a[::-1])
23. By Sneha Sharma
Made by Sneha Sharma. Decimal to binary conversion python program using recursion. Source
23 your decimal number:10111
def dec_to_binary(n):
if n>1:
dec_to_binary(n//2)
print(n%2,end='')
num=int(input("your decimal number:"))
dec_to_binary(num)
print(" ")
24. By Seniru
Made by Seniru. Source
Input: 24 Output: 11000
import math
def decToBin(n):
bin = []
while n >= 1:
bin.append(math.floor(n%2))
n = n/2
return str(bin[::-1])
cin = int(input("Input: "))
print(str(cin)+"\nOutput: "+str(decToBin(cin).replace(" ","").replace("[","") .replace("]","").replace(",","")))
25. By Rik Wittkopp
Made by Rik Wittkopp. Source
Decimal: 25 => Binary: 11001
num = int(input() or 72)
lst =[]
def _bin(x):
exp = 0
calc = 0
if x ==0:
return lst
else:
while True:
calc = 2**exp
exp +=1
if calc > x:
exp -=2
calc = 2**exp
break
lst.append('1'+('0'* exp))
x -= calc
return _bin(x)
_bin(num)
print(f'Decimal: {num} => Binary: {sum(int(i) for i in lst)}')
26. By Sapphire
Made by Sapphire. Source
Enter a value to convert to binary: 26 Converted Binary: 11010
#S_notes: simple conversion function.
# the function does not check for incorrect
# entries.
def convert_to_binary(value):
"""Converts a decimal value to a binary
string and returns it.
Perm: value = integer value"""
result = ''
while value > 0:
if value % 2 == 0:
result += '0'
else:
result += '1'
value //= 2
# returns the string value in reverse order
return result[::-1]
u_input = input('Enter a value to convert to binary: ')
print(u_input)
bin_result = convert_to_binary(int(u_input))
print("\nConverted Binary:", bin_result)
27. By Supersebi3
Made by Supersebi3. Python Decimal to Binary Program using Just 4 lines of Code. Source
27 11011
x = int(input())
b = bin(x)
b = b.split("b")
print(b[1])
28. By Sam
Made by Sam. Source
Enter a decimal number 28 The binary form of 28 is 11100
decimal = int(input("Enter a decimal number"))
decimal
binary = bin(decimal).replace("0b","")
print("\nThe binary form of "+ str(decimal) + " is "+ binary)
29. By ICE
Made by ICE. Source
Result: Your number in decimal is 29 , in binary is 11101
n=int(input("Result: "))
print("Your number in decimal is",n,", in binary is",end=" ")
s=[]
while(n!=0):
s.append(str(n%2))
n=int(n/2)
cont=-1
while(cont!=-(len(s)+1)):
print(s[cont],end="")
cont=(cont-1)
30. By Luxshan Thuraisingam
Made by Luxshan Thuraisingam. Source
enter an integer:30 binary number: 11110
x=int(input("enter an integer:"))
bn=""
while x>1:
quotient=int(x/2)
remainder=x%2
bn=str(remainder)+bn
x=quotient
bn=str(x)+bn
print("binary number:",bn)