Skip to content
Home » Python Programs » Decimal to Binary in Python Using While Loop

Decimal to Binary in Python Using While Loop

Simple python program to convert decimal to binary. Python program that converts a decimal value to its binary equivalent using the Python programming language. In this approach, we take a decimal integer as input and divide it by two until it equals zero. The binary bumber is then formed by concatenating the remainders obtained as a consequence of the division in a bottom-up fashion. Using this program we can execute the python program to convert decimal to binary for beginners.

In mathematics, a binary number system is a positional numeral system that uses 2 as the basis and hence requires only two distinct symbols for its digits, 0 and 1, rather than the typical ten different symbols required in the decimal system. A binary number is made up of two numbers: 0s and 1s. Binary numbers have a base of 2 and are expressed as such. For instance, (101)2 (101) 2. Each digit in a binary integer is known as a bit. Simple definition for python program to convert decimal to binary.

A decimal number is defined in algebra as a number with a decimal point separating the entire number and fractional parts. A decimal point is the dot in a decimal number. The digits after the decimal point represent a number less than one. This is simple definition for python program to convert decimal to binary.

Python Program to Convert Decimal to Binary

decimal = int(input("Enter the Decimal Number \n"))
binary = 0
ctr = 0
temp = decimal  
while(temp > 0):
    binary = ((temp%2)*(10**ctr)) + binary
    temp = int(temp/2)
    ctr += 1
print("Binary of {x} is: {y}".format(x=decimal,y=binary))

Output for python program to convert decimal to binary:

Enter the Decimal Number
45
Binary of 45 is: 101101