0
0
PythonProgramBeginner · 2 min read

Python Program to Find Binary Equivalent of Number

Use the built-in function bin(number) to get the binary equivalent of a number as a string prefixed with '0b', or use format(number, 'b') to get it without the prefix.
📋

Examples

Input5
Output101
Input0
Output0
Input18
Output10010
🧠

How to Think About It

To find the binary equivalent of a number, repeatedly divide the number by 2 and record the remainders. The binary digits are the remainders read in reverse order. Alternatively, use Python's built-in functions to convert directly.
📐

Algorithm

1
Get the input number.
2
If the number is zero, return '0' as the binary equivalent.
3
Otherwise, divide the number by 2 repeatedly and store the remainder each time.
4
Collect all remainders in reverse order to form the binary string.
5
Return the binary string.
💻

Code

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

number = int(input('Enter a number: '))
print('Binary equivalent:', decimal_to_binary(number))
Output
Enter a number: 5 Binary equivalent: 101
🔍

Dry Run

Let's trace the input 5 through the code

1

Start with number 5

num = 5, binary = ''

2

Calculate remainder and update binary

5 % 2 = 1, binary = '1', num = 5 // 2 = 2

3

Next iteration

2 % 2 = 0, binary = '0' + '1' = '01', num = 2 // 2 = 1

4

Next iteration

1 % 2 = 1, binary = '1' + '01' = '101', num = 1 // 2 = 0

5

Loop ends, return binary

binary = '101'

numnum % 2binarynum // 2
5112
20011
111010
💡

Why This Works

Step 1: Check for zero

If the input number is zero, return '0' immediately because its binary is simply 0.

Step 2: Divide and find remainder

Divide the number by 2 and find the remainder; this remainder is the least significant bit of the binary number.

Step 3: Build binary string

Prepend each remainder to the binary string to build the binary number from least to most significant bit.

🔄

Alternative Approaches

Using built-in bin() function
python
number = int(input('Enter a number: '))
binary = bin(number)[2:]
print('Binary equivalent:', binary)
This is the simplest and fastest method but returns a string with a '0b' prefix which we remove by slicing.
Using format() function
python
number = int(input('Enter a number: '))
binary = format(number, 'b')
print('Binary equivalent:', binary)
This method directly formats the number as binary without any prefix.

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

Time Complexity

The time depends on the number of bits in the number, which is proportional to log base 2 of the number.

Space Complexity

The space needed is proportional to the number of bits to store the binary string.

Which Approach is Fastest?

Using built-in functions like bin() or format() is fastest and simplest compared to manual division.

ApproachTimeSpaceBest For
Manual divisionO(log n)O(log n)Learning binary conversion logic
bin() functionO(log n)O(log n)Quick and easy conversion
format() functionO(log n)O(log n)Clean binary string without prefix
💡
Use bin(number)[2:] or format(number, 'b') for quick binary conversion in Python.
⚠️
Beginners often forget to remove the '0b' prefix when using bin() and get unexpected output.