Decimal to binary conversion in python by R4R Team

Binary number
-It is a number which expressed in the base-2 numeral system,
-Binary numbers are 0 or 1.

Decimal number
- A number which expressed in the base-10 numeral system.
- Decimal number range is 0 to 9

Conversion of Decimal to binary number
Example-
Decimal number - 10
Its binary equivalent is 1010

Decimal number - 11
Its binary equivalent is 1011

Program -

print("Enter decimal number")
n=int(input())
binary=""
l=[]

#break decimal number into binary bits
while(n>0):
t=n%2
l.append(t)
n=int(n/2)

i=len(l)-1

#traverse list in reverse order
while(i>=0):
binary=binary+str(l[i])
i=i-1

print("Its binary equivalent is - "+binary)


output -

Enter decimal number
10
Its binary equivalent is - 1010

Enter decimal number
32
Its binary equivalent is - 100000

Enter decimal number
63
Its binary equivalent is - 111111


-In this program, firstly we take input of decimal number then using 1st while loop, we break the decimal into binary bits and store those bit in list and in 2nd while loop, we traverse the list in reverse order and add to 'binary' variable and this variable have the binary equivalent of given number.

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!