{"id":19087,"date":"2021-08-29T15:13:26","date_gmt":"2021-08-29T09:43:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=19087"},"modified":"2021-11-22T18:37:09","modified_gmt":"2021-11-22T13:07:09","slug":"python-program-to-convert-binary-to-decimal-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-binary-to-decimal-using-recursion\/","title":{"rendered":"Python Program to Convert Binary to Decimal using Recursion"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Sign Change<\/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 binary string to a decimal number 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 binary string to a decimal number 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 for Sign Change 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), Inorder \/Preorder\/Postorder Tree Traversals, …<\/p>\n
\nRecursion:<\/strong><\/p>\nGiven binary number = 1011<\/pre>\n
The decimal Equivalent of the given binary number { 1011 } is : 11<\/pre>\n
Given binary number = 10010101<\/pre>\n
The decimal Equivalent of the given binary number { 10010101 } is : 149<\/pre>\n
Program to Convert Binary to Decimal using Recursion in Python<\/h2>\n
\n
Method #1: Using Recursion (Static Input)<\/h3>\n
\n
# Create a recursive function to say binaryToDeci()\r\n# which accepts the binary number as an argument\r\n# and returns the decimal equivalent of the given binary string.\r\n\r\n\r\ndef binaryToDeci(binanumb):\r\n # Inside the binaryToDeci()\u00a0function,\r\n # Check if the binary number is equal to 0 using the if conditional statement.\r\n if(binanumb == 0):\r\n # If it is true then return 0.\r\n return 0\r\n # Else return (n% 10 + 2* binaryToDeci(n \/\/ 10))\r\n # {Recursive logic where n%10 gives rightmostbit and n\/\/10 divides the number by 10}.\r\n return (binanumb % 10 + 2 * binaryToDeci(binanumb \/\/ 10))\r\n\r\n\r\n# Give the binary number as static input and store it in a variable.\r\ngvnbinarynumb = 1011\r\n# Inside the main function pass the given binary number as an argument\r\n# to binaryToDeci()\u00a0which returns the decimal equivalent of the given binary number.\r\nresdecimalnumbr = binaryToDeci(gvnbinarynumb)\r\n# Print the decimal equivalent of the given binary number.\r\nprint(\r\n 'The decimal Equivalent of the given binary number {', gvnbinarynumb, '} is :', resdecimalnumbr)\r\n<\/pre>\n
The decimal Equivalent of the given binary number { 1011 } is : 11<\/pre>\n
Method #2: Using Recursion (User Input)<\/h3>\n
\n
# Create a recursive function to say binaryToDeci()\r\n# which accepts the binary number as an argument\r\n# and returns the decimal equivalent of the given binary string.\r\n\r\n\r\ndef binaryToDeci(binanumb):\r\n # Inside the binaryToDeci()\u00a0function,\r\n # Check if the binary number is equal to 0 using the if conditional statement.\r\n if(binanumb == 0):\r\n # If it is true then return 0.\r\n return 0\r\n # Else return (n% 10 + 2* binaryToDeci(n \/\/ 10))\r\n # {Recursive logic where n%10 gives rightmostbit and n\/\/10 divides the number by 10}.\r\n return (binanumb % 10 + 2 * binaryToDeci(binanumb \/\/ 10))\r\n\r\n\r\n# Give the binary number as user input using int(input()) function\r\n# and store it in a variable.\r\ngvnbinarynumb = int(input('Enter some random binary number = '))\r\n# Inside the main function pass the given binary number as an argument\r\n# to binaryToDeci()\u00a0which returns the decimal equivalent of the given binary number.\r\nresdecimalnumbr = binaryToDeci(gvnbinarynumb)\r\n# Print the decimal equivalent of the given binary number.\r\nprint(\r\n 'The decimal Equivalent of the given binary number {', gvnbinarynumb, '} is :', resdecimalnumbr)\r\n<\/pre>\n
Enter some random binary number = 10010101\r\nThe decimal Equivalent of the given binary number { 10010101 } is : 149<\/pre>\n
\n