Recursive Program for Binary to Decimal Last Updated : 30 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given a binary number as string, find its decimal equivalent. Examples: Input : binary = "101"Output : 5 Input : binary = "1111"Output : 15 We have discussed iterative solution to convert Binary to Decimal.The idea is simple, we add current term and recur for remaining terms. C++ // Recursive CPP program to convert binary // decimal #include<bits/stdc++.h> using namespace std; int toDecimal(string binary, int i=0) { // If we reached last character int n = binary.length(); if (i == n-1) return binary[i] - '0'; // Add current tern and recur for // remaining terms return ((binary[i] - '0') << (n-i-1)) + toDecimal(binary, i+1); } // Driver code int main() { string binary = "1010"; cout << toDecimal(binary) << endl; return 0; } Java // Recursive Java program to convert binary // decimal class GFG { static int toDecimal(String binary,int i) { // If we reached last character int n = binary.length(); if (i == n-1) return binary.charAt(i) - '0'; // Add current tern and recur for // remaining terms return ((binary.charAt(i) - '0') << (n-i-1)) + toDecimal(binary, i+1); } // Driver code public static void main(String []args) { String binary = "1010"; int i=0; System.out.println(toDecimal(binary,i)); } } // This code is contributed // by ihritik ( Hritik Raj) Python3 # Recursive Python3 program to convert # binary decimal def toDecimal(binary, i = 0): # If we reached last character n = len(binary) if (i == n - 1) : return int(binary[i]) - 0 # Add current tern and recur for # remaining terms return (((int(binary[i]) - 0) << (n - i - 1)) + toDecimal(binary, i + 1)) # Driver code if __name__ == "__main__" : binary = "1010" print(toDecimal(binary)) # This code is contributed by Ryuga C# // Recursive C# program to convert binary // decimal using System; class GFG { static int toDecimal(string binary, int i=0) { // If we reached last character int n = binary.Length; if (i == n-1) return binary[i] - '0'; // Add current tern and recur for // remaining terms return ((binary[i] - '0') << (n-i-1)) + toDecimal(binary, i+1); } // Driver code public static void Main() { string binary = "1010"; Console.WriteLine(toDecimal(binary)); } } // This code is contributed // by ihritik ( Hritik Raj) PHP <?php // Recursive CPP program to convert // binary decimal function toDecimal($binary, $i = 0) { // If we reached last character $n = strlen($binary); if ($i == $n - 1) return ord($binary[$i]) - ord('0'); // Add current tern and recur for // remaining terms return ((ord($binary[$i]) - ord('0')) << ($n - $i - 1)) + toDecimal($binary, $i + 1); } // Driver code $binary = "1010"; echo toDecimal($binary) . "\n"; // This code is contributed by ita_c ?> JavaScript <script> // Recursive JavaScript program to convert binary // decimal function toDecimal(binary,i) { // If we reached last character let n = binary.length; if (i == n-1) return binary[i] - '0'; // Add current tern and recur for // remaining terms return ((binary[i] - '0') << (n-i-1)) + toDecimal(binary, i+1); } // Driver code let binary = "1010"; let i=0; document.write(toDecimal(binary,i)); // This code is contributed by avanitrachhadiya2155 </script> Output: 10 Time complexity: O(N) where N is the length of binary stringAuxiliary space: O(N) for recursive stack space. Comment More infoAdvertise with us Next Article Recursive Program for Binary to Decimal K KelechiOgudu1 Follow Improve Article Tags : Recursion C++ Programs DSA Practice Tags : Recursion Similar Reads C++ Program For Decimal To Binary Conversion Binary Numbers uses only 0 and 1 (base-2), while Decimal Number uses 0 to 9 (base-10). In this article, we will learn to implement a C++ program to convert Decimal numbers to Binary Numbers. The below diagram shows an example of converting the decimal number 17 to an equivalent binary number. Recomm 3 min read C++ Program For Binary To Decimal Conversion The binary number system uses only two digits 0 and 1 to represent an integer and the Decimal number system uses ten digits 0 to 9 to represent a number. In this article, we will discuss the program for Binary to Decimal conversion in C++. Algorithm to Convert Binary Numbers to DecimalInitialize a v 4 min read C++ Program For Decimal To Octal Conversion The octal numbers are a base 8 number system that uses digits from 0-7 and the decimal numbers are a base 10 numbers system that uses 10 digits from 0-9 to represent any numeric value. In this article, we will learn how to write a C++ program to convert a given decimal number into an equivalent octa 2 min read C++ Program For Binary To Octal Conversion The problem is to convert the given binary number (represented as a string) to its equivalent octal number. The input could be very large and may not fit even into an unsigned long long int. Examples: Input: 110001110Output: 616 Input: 1111001010010100001.010110110011011Output: 1712241.26633 Simple 5 min read C++ Program For Hexadecimal To Decimal Conversion The hexadecimal numbers are base 16 numbers that use 16 symbols {0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} to represent all digits. Here, (A, B, C, D, E, F) represents (10, 11, 12, 13, 14, 15). Decimal numbers are base 10 numbers with 10 symbols to represent all digits. In this article, we will l 3 min read First and Last Three Bits Given an integer N. The task is to print the decimal equivalent of the first three bits and the last three bits in the binary representation of N.Examples: Input: 86 Output: 5 6 The binary representation of 86 is 1010110. The decimal equivalent of the first three bits (101) is 5. The decimal equival 13 min read Balanced Ternary Number System As we already know, a Binary number system is a number system that has only 2 digits in it, i.e. 0 and 1. Similarly, we also know that a Ternary number system is a number system that has only 3 digits in it, i.e. 0, 1, and 2. In this article, we will learn about Balanced Ternary Number System. A bal 5 min read Program for Binary To Decimal Conversion Given a binary number as input, we need to write a program to convert the given binary number into an equivalent decimal number.Examples : Input : 111Output : 7Explanation : The output of 7 for input 111 represents the decimal equivalent of the binary number 111.Input : 1010Output : 10Explanation : 15 min read Program for Decimal to Binary Conversion Given a decimal number n, the task is to convert the given decimal number into an equivalent binary number.Examples:Â Input: n = 12Output: "1100"Explanation: Input: n = 17Output: "10001"Explanation: Table of Content[Approach - 1] Division by 2 - O(log n) Time and O(1) Space[Approach - 2] Using Head R 11 min read Decimal to binary number using recursion Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent binary number. Examples : Input: d = 7 Output: 111Explanation: 20 + 21 + 22 = 1+2+4 = 7.Input: d = 10Output: 1010Explanation: 21 + 23 = 2+8 = 10.We previously discussed an iterative app 4 min read Like