{"id":19091,"date":"2021-08-29T15:13:29","date_gmt":"2021-08-29T09:43:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=19091"},"modified":"2021-11-22T18:37:09","modified_gmt":"2021-11-22T13:07:09","slug":"python-program-to-convert-decimal-number-to-binary-string-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-decimal-number-to-binary-string-using-recursion\/","title":{"rendered":"Python Program to Convert Decimal Number to Binary String using Recursion"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Convert Binary to Decimal using Recursion<\/a> Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder \/Preorder\/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.<\/p>\n Binary Code:<\/strong><\/p>\n As previously stated, Binary Code is a Base-2 representation of a number. In Binary, all numbers are represented by simply two symbols: 0 and 1. Binary (also known as base-2) is a numerical system with only two digits: 0 and 1.<\/p>\n Given a binary string, the task is to convert the given decimal number to a binary string using recursion in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to convert the given decimal number to a binary string using recursion in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Convert Binary to Decimal using Recursion Recursion: Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), …<\/p>\n
\nRecursion:<\/strong><\/p>\nGiven binary number = 153<\/pre>\n
The decimal Equivalent of the given binary number { 153 } is : 10011001<\/pre>\n
Given binary number = 408<\/pre>\n
The decimal Equivalent of the given binary number { 408 } is : 110011000<\/pre>\n
Program to Convert Decimal Number to Binary String using Recursion in Python<\/h2>\n
\n
Method #1: Using Recursion (Static Input)<\/h3>\n
\n
# Create a recursive function to say deciToBinary() which accepts\r\n# the decimal number as an argument and returns the binary equivalent\r\n# of the given decimal number.\r\n\r\n\r\ndef deciToBinary(decinumb):\r\n # Inside the deciToBinary()\u00a0function,\r\n # Check if the decimal number is equal to 0 using the if conditional statement.\r\n if(decinumb == 0):\r\n # If it is true then return 0.\r\n return 0\r\n # Else return (n% 2 + 10* deciToBinary(n \/\/ 2)) {Recursive logic}\r\n return (decinumb % 2 + 10 * deciToBinary(decinumb \/\/ 2))\r\n\r\n\r\n# Give the decimal number as static input and store it in a variable.\r\ngvndecimalnumb = 153\r\n# Inside the main function pass the given decimal number as an argument\r\n# to deciToBinary()\u00a0which returns the\r\n# binary equivalent of the given decimal number.\r\nresbinarynumbr = deciToBinary(gvndecimalnumb)\r\n# Print the decimal equivalent of the given binary number.\r\nprint(\r\n 'The decimal Equivalent of the given binary number {', gvndecimalnumb, '} is :', resbinarynumbr)\r\n<\/pre>\n
The decimal Equivalent of the given binary number { 153 } is : 10011001<\/pre>\n
Method #2: Using Recursion (User Input)<\/h3>\n
\n
# Create a recursive function to say deciToBinary() which accepts\r\n# the decimal number as an argument and returns the binary equivalent\r\n# of the given decimal number.\r\n\r\n\r\ndef deciToBinary(decinumb):\r\n # Inside the deciToBinary() function,\r\n # Check if the decimal number is equal to 0 using the if conditional statement.\r\n if(decinumb == 0):\r\n # If it is true then return 0.\r\n return 0\r\n # Else return (n% 2 + 10* deciToBinary(n \/\/ 2)) {Recursive logic}\r\n return (decinumb % 2 + 10 * deciToBinary(decinumb \/\/ 2))\r\n\r\n\r\n# Give the binary number as user input using the int(input()) function and store it in a variable.\r\ngvndecimalnumb = int(input('Enter some random decimal number = '))\r\n# Inside the main function pass the given decimal number as an argument\r\n# to deciToBinary() which returns the\r\n# binary equivalent of the given decimal number.\r\nresbinarynumbr = deciToBinary(gvndecimalnumb)\r\n# Print the decimal equivalent of the given binary number.\r\nprint(\r\n 'The decimal Equivalent of the given binary number {', gvndecimalnumb, '} is :', resbinarynumbr)\r\n<\/pre>\n
Enter some random decimal number = 408\r\nThe decimal Equivalent of the given binary number { 408 } is : 110011000<\/pre>\n
\n