Find binary equivalent of a number recursively in Python
Today, we will learn to find the binary equivalent of a number recursively in Python. A binary number is a number that follows the base-2 numeral system. Any binary number consists of only 0’s and 1’s. Here, we will get to know the conversion of a decimal number to binary format. Also, if you want a Python program that finds the binary equivalent of a decimal number using a recursive function, you are in the right place.
Binary and Decimal numbers
A binary number is a sequence of 0’s and 1’s only. It is represented using a base-2 numeral system i.e. digits 0 and 1. Every single digit of a binary number is called a bit.
For example –> 101011
A decimal number contains digits 0-9. We represent a decimal number using a base-10 numeral system. Every decimal number contains two parts i.e. a whole and a fraction part. The whole and fraction part is divided by a decimal dot. But to keep it simple, in this tutorial, we will consider decimal numbers with the whole part only.
For example –> 106 is a decimal number with 0 fraction part.
Decimal to binary conversion using recursive function
Now, we will learn how to convert the given decimal number to a binary number. For converting a decimal number, we will follow the steps given below-
- Store the decimal number in ‘num’.
- If the value of ‘num’ is greater than 1, go to step 5.
- Divide the number by 2 to get the remainder.
- Display the remainder and return.
- Divide the number by 2, go to step 2 and repeat.
Here, we will implement the above steps using a recursive function as follows-
def convert_binary(num): if num > 1: convert_binary(num//2) print(num % 2,end = '')
To understand the above recursive function, let’s take an example. Consider a decimal number 106. So, the above function finds its binary equivalent as follows-
So, this recursive function prints the binary number bit by bit.
Python program to find binary equivalent of a decimal number recursively
So, now we will see a Python program that converts a given decimal number to its equivalent binary format using a recursive function. Firstly, we take the decimal number from the user as input. Then, we make a call to the recursive function ‘convert_binary’. We pass this decimal number as an argument. Finally, the function displays the binary equivalent of that number bit by bit onto the screen as output. So, the Python program is as follows-
def convert_binary(num): if num > 1: convert_binary(num//2) print(num % 2,end = '') decimal = int(input("ENTER A DECIMAL NUMBER : ")) print("THE BINARY EQUIVALENT OF %d IS : "%decimal,end='') convert_binary(decimal)
Python program output
The above program displays the binary equivalent of the given decimal number. The output of this program is-
siddharth@siddharth-Lenovo-Y520-15IKBN:~/python$ python3 binary.py ENTER A DECIMAL NUMBER : 8 THE BINARY EQUIVALENT OF 8 IS : 1000 siddharth@siddharth-Lenovo-Y520-15IKBN:~/python$
So, the binary equivalent of decimal number 8 is 1000.
Thank you for reading this tutorial. I hope it helps you.
Also, read: Convert binary to gray code in Python