0
0
PythonProgramBeginner · 2 min read

Python Program to Convert Decimal to Binary

You can convert a decimal number to binary in Python using bin(number)[2:] or by writing a function that divides the number by 2 repeatedly and collects remainders.
📋

Examples

Input0
Output0
Input10
Output1010
Input255
Output11111111
🧠

How to Think About It

To convert a decimal number to binary, repeatedly divide the number by 2 and record the remainder each time. The binary number is the remainders read in reverse order. Alternatively, Python's built-in bin() function can be used and the first two characters removed.
📐

Algorithm

1
Get the decimal number as input.
2
If the number is 0, return '0' as binary.
3
While the number is greater than 0, divide it by 2 and store the remainder.
4
Collect all remainders in reverse order to form the binary string.
5
Return the binary string.
💻

Code

python
def decimal_to_binary(n):
    if n == 0:
        return '0'
    binary = ''
    while n > 0:
        binary = str(n % 2) + binary
        n = n // 2
    return binary

number = 10
print(decimal_to_binary(number))
Output
1010
🔍

Dry Run

Let's trace the decimal number 10 through the code.

1

Initial number

n = 10, binary = ''

2

First division

10 % 2 = 0, binary = '0', n = 10 // 2 = 5

3

Second division

5 % 2 = 1, binary = '1' + '0' = '10', n = 5 // 2 = 2

4

Third division

2 % 2 = 0, binary = '0' + '10' = '010', n = 2 // 2 = 1

5

Fourth division

1 % 2 = 1, binary = '1' + '010' = '1010', n = 1 // 2 = 0

6

End loop

n = 0, stop loop, return '1010'

n before divisionRemainder (n % 2)binary stringn after division (n // 2)
10005
51102
200101
1110100
💡

Why This Works

Step 1: Divide and get remainder

Each division by 2 gives a remainder of 0 or 1, which is a binary digit.

Step 2: Build binary string backwards

We add each remainder to the front of the string because the first remainder is the least significant bit.

Step 3: Stop when number is zero

When the number becomes zero, all binary digits are collected and the loop ends.

🔄

Alternative Approaches

Using built-in bin() function
python
number = 10
binary = bin(number)[2:]
print(binary)
This is the simplest and fastest way but returns a string with '0b' prefix which we remove by slicing.
Using recursion
python
def decimal_to_binary_recursive(n):
    if n == 0:
        return ''
    return decimal_to_binary_recursive(n // 2) + str(n % 2)

number = 10
binary = decimal_to_binary_recursive(number) or '0'
print(binary)
This method uses recursion to build the binary string but may be less efficient for large numbers.

Complexity: O(log n) time, O(log n) space

Time Complexity

The loop runs once for each binary digit, which is proportional to log base 2 of the number.

Space Complexity

The binary string stores one character per binary digit, so space grows with log base 2 of the number.

Which Approach is Fastest?

Using Python's built-in bin() is fastest and simplest; manual methods are educational but slower.

ApproachTimeSpaceBest For
Manual division loopO(log n)O(log n)Learning and understanding binary conversion
Built-in bin() functionO(1) practicallyO(log n)Quick and efficient conversion
Recursive methodO(log n)O(log n)Understanding recursion, less efficient for large inputs
💡
Use bin(number)[2:] for a quick decimal to binary conversion in Python.
⚠️
Forgetting to reverse the collected remainders or adding them in the wrong order.