Convert Decimal to Binary Using Recursion in Python



In this article, we will show you how to convert decimal to binary using recursion in python.

A decimal number is the most familiar number system to the general public. It is base 10 which has only 10 symbols ? 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Whereas Binary number is the most familiar number system to digital systems, networking, and computer professionals. It is base 2 which has only 2 symbols: 0 and 1, these digits can be represented by off and on respectively.

When we convert a number from the decimal number system to the binary number system, we are using decimal to binary-conversion. The total number of digits used in the number system determines the base of all number systems. The binary number system, for example, has a base of two because it only uses two digits to represent a number. Similarly, the decimal number system has a base of ten because a number is represented by ten digits.

Using Recursion(First Logic)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task ?

  • Create a recursive function to getBinaryForm() using the def keyword to convert the decimal number passed to it as an argument into a binary form.

  • Use the if conditional statement to check whether the number passed is equal to 0 with the == operator.

  • Return 0 if the condition is true i.e, the decimal number passed is 0.

  • Else return the binary form of the decimal number passed to the function using recursive logic( get the last bit of the number using the modulus operator(%) and divide the number by 2(half) and multiply it with 10 and Cal the recursive function again with this value-added).

  • Create a variable to store the input number.

  • Call the getBinaryForm() function by passing the input decimal number as an argument and print the resultant binary equivalent of the decimal number returned by the function.

Example

The following program returns the binary form of a decimal number using recursion ?

# creating a function to convert decimal number passed to it # as an argument into a binary form def getBinaryForm(decimalnum): # checking whether the number passed is equal to 0 if decimalnum == 0: # returning 0 if the number passed is 0 return 0 else: # Else getting the last bit of the number and dividing the number by 2(half) and multiplying it with 10 # Calling the recursive function again with this value-added return (decimalnum % 2 + 10 * getBinaryForm(int(decimalnum // 2))) # input decimal number decimalnum = 5 print("The binary equivalent of",decimalnum,"is:") # calling the getBinaryForm() function by passing the decimal number as an argument print(getBinaryForm(decimalnum))

Output

On executing, the above program will generate the following output &minnus;

The binary equivalent of 5 is:
101

Using Recursion(Second Logic)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task ?

  • Create a recursive function to getBinaryForm() using the def keyword to convert the decimal number passed to it as an argument into a binary form

  • Use the if conditional statement to check whether the number passed is equal to 0 with the == operator.

  • Return 0 if the condition is true i.e, the decimal number passed is 0.

  • Call the function recursively again by passing the given number by half and store this result in a variable.

  • Get the last bit of the given decimal number using the modulus operator(%) and add 10* to the above result.

  • Create a variable to store the input number.

  • Call the getBinaryForm() function by passing the input decimal number as an argument and print the resultant binary equivalent of the decimal number returned by the function.

Example

The following program returns the binary form of a decimal number using recursion ?

# creating a function to convert decimal number passed to it # as an argument into a binary form def getBinaryForm(decimalnum): # checking whether the number passed is equal to 0 if decimalnum == 0: # returning 0 if the number passed is 0 return 0 # Call the function recursively again by passing the given number by half result = getBinaryForm(decimalnum // 2) # Getting the last bit and multiply the result with 10 return decimalnum % 2 + 10 * result # input decimal number decimalnum = 500 print("The binary equivalent of",decimalnum,"is:") # calling the getBinaryForm() function by passing # the decimal number as an argument print(getBinaryForm(decimalnum))

Output

On executing, the above program will generate the following output ?

The binary equivalent of 500 is:
111110100

Conclusion

We learned two different methods for calculating the binary format of a given decimal number using recursion in this article. We learned how to invoke the recursive function by passing it some value (result). We also learned how to divide a number by half to get only the integer number.

Updated on: 2022-10-27T12:37:31+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements