Decimal to Binary Algorithm in Python

What are Decimal Numbers?

Decimal Numbers are the number system that uses 10 digits, from 0 to 9. The base of the decimal number system is 10. It is also known as the base-10 number system. It is used to form digits with different combinations. Each digit becomes 10 times more significant in the decimal numbers while moving from the unit place.

For example: (345)10 = 3 x 102 + 4 x 101 + 5 x 100

What are Binary Numbers?

Binary numbers use two digits, 0 and 1. The base of the binary numbers is 2. It is also known as the base-2 number system. The combination of the binary digits is used to form different numbers. Each digit is 2 times more significant in binary numbers starting from the unit place.

For example: (11011)2 = 1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20

Converting Decimal to Binary Numbers in Python

Problem Statement

We have decimal numbers and need to convert them into binary numbers using Python.

Let's understand the problem statement using examples.

Example 1:

Input:

Output:

1111011

Example 2:

Input:

Output:

10001110

There are different approaches to converting decimal numbers to binary numbers.

Approach 1: Recursion

Algorithm:

  1. Define a function
  2. Check if n > 1
  3. Convert the decimal number using n % 2
  4. Print each number at every iteration
  5. Take input from the user
  6. Call the function
  7. Print result

Program:

Code:

Output:

Enter the decimal number: 123
1 1 1 1 0 1 1

Explanation:

We made a function DecToBin( ), which recursively converts the decimal number to binary. It will first check if the number is greater than 1. Then, it recursively calls the n // 2. Then, it will print the binary equivalent of the number (n % 2). It will take the number from the user and call the function, which converts the decimal to binary number.

Approach 2: Built-in Function to convert decimal to binary

Algorithm:

  1. Define a function.
  2. Return the built-in function bin.replace( ).
  3. Call the function object.
  4. Print the output.

Program:

Code:

Output:

Enter a decimal number : 123
1111011

Explanation:

We made a function, DecToBin( ), which converts the decimal number to a binary number. Then, using the built-in bin( ) function, we converted the decimal number to a binary number. We then called the function and printed the output.

Approach 3: Using the built-in format( ) function.

Algorithm:

  1. Define a function.
  2. Using the format( ) function, convert the decimal number to binary
  3. Call the function
  4. Print the output

Program:

Code:

Output:

Enter a decimal number : 1445
10110100101

Explanation:

We made a function, DecToBin( ), which converts the decimal number to a binary number. Then, we used the format( ) function with argument {0:b}. We take the input from the user and call the function to print the output.

Or We can use the format function in another way:

Program:

Code:

Output:

Enter a decimal number : 198
11000110

Explanation:

We first made the DecToBin () function to convert the decimal to a binary number. Then, using the format( ) function, we converted the decimal number to a binary number. We take the input from the user and call the function to print the output.

Approach 4: One Line code to convert decimal numbers to binary numbers

Algorithm:

This is a one-liner code to convert decimal to binary numbers. It is also known as the Quick Ninja Method.

Program:

Code:

Output:

Enter a decimal number : 122345
11101110111101001

Explanation:

We used the quick ninja method to convert decimal numbers to binary numbers. We take the input from the user and use the bin( ) function to convert the decimal number to a binary number.

Approach 5: Bitwise Shift Operator to convert Decimal to Binary number

Algorithm:

  1. Define a function.
  2. Return 0 if num == 0
  3. Initiate a While loop
  4. Using the bitwise Shift operator, convert the number into binary equivalent
  5. Take input from the user
  6. Call the function
  7. Print the output

Code:

Output:

Enter a decimal number : 1998
11111001110 

Explanation:

We made a function, DecToBin( ), which converts the decimal number to a binary number. Then, we initiated a while loop in which the decimal number is converted to a binary number using the bitwise right shift operator. Then, we take input from the user and call the function to print the output.