In this tutorial, we will learn how to convert a decimal number to a binary number using recursion in Python. The binary number system is a base-2 number system, meaning it only uses two symbols, 0 and 1, to represent all its values. Converting a decimal number to binary is a common operation in computer science, and it can be useful in many areas such as data storage and transmission.
Python Program to Convert Decimal to Binary Using Recursion
This code uses a recursive function in Python that converts a decimal number to its binary equivalent. The function is called find(), and it takes a single argument, which is the decimal number to be converted. We prompt the user to enter a decimal number, convert the input string to an integer, and call the find() function with the integer as the argument. We then print the binary equivalent of the decimal number.
The process of converting a decimal number to binary involves repeatedly dividing the decimal number by 2 and keeping track of the remainder. The remainders form the binary equivalent of the decimal number, with the least significant bit (LSB) on the right and the most significant bit (MSB) on the left.
The find() function begins by checking whether the decimal number is equal to 0. If it is, the function returns 0, which is the binary equivalent of 0. If the decimal number is not 0, the function proceeds to calculate the binary equivalent of the decimal number.
To accomplish this, the function initially determines the remainder by dividing the decimal number by 2 using the modulo operator (%). Subsequently, we add this remainder to 10 times the binary equivalent of the integer division of the decimal number by 2.
The binary equivalent of the integer division of the decimal number by 2 is recursively calculated by invoking the find() function once again, passing the decimal number divided by 2 as the argument. This process continues until the decimal number is equal to 0, at which point the function returns the binary equivalent as a string.
Let’s test the code by entering different decimal numbers and see the results:
# Convert Decimal to Binary in Python Using Recursion def find( decimal_number ): if decimal_number == 0: return 0 else: return (decimal_number % 2 + 10 * find(int(decimal_number // 2))) # we are taking a number from user as input # entered value will be converted to int from string decimal_number=int(input("Enter the decimal number:")) print("the Binary number of ", decimal_number," is ", find(decimal_number))
Enter the decimal number:24 the Binary number of 24 is 11000
The user inputs the decimal number 24 into the program, which then passes the value to the find() function. Using recursion, the find() function converts the decimal number to its binary equivalent. The function works by first checking if the decimal number is equal to 0. As the value of 24 is not equal to 0, the function proceeds to determine the remainder when dividing 24 by 2, which is 0. It then recursively invokes itself, utilizing the integer division of 24 by 2 as the argument.This process continues until the decimal number becomes 0, at which point the function returns 0.
In this case, the function makes four recursive calls before the decimal number becomes 0. During each call, the function calculates the remainder of the decimal number when divided by 2 and adds it to 10 times the binary equivalent of the integer division of the decimal number by 2. After the final recursive call, the function returns the binary equivalent of the original decimal number, which in this case is 11000. The program then prints out “the Binary number of 24 is 11000” to display the final result.
In this tutorial, we learned how to convert a decimal number to a binary number in Python using recursion. We defined a recursive function that takes a decimal number as an argument and returns its binary equivalent. We then went through the steps to convert a decimal number to binary, including checking for a base case and defining the recursive case.
Then we wrote the Python code to convert a decimal number to binary using recursion. By understanding the underlying concepts. We saw the advantages and limitations of using recursion, and also learned some tips for using recursion effectively. Recursion is a powerful technique that can simplify code and make it easier to understand, but it’s essential to use it correctly to avoid common pitfalls like the stack
For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions