Recursive Program for Binary to Decimal
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; } |
chevron_right
filter_none
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) |
chevron_right
filter_none
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 |
chevron_right
filter_none
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) |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
Output:
10
Recommended Posts:
- Program for Binary To Decimal Conversion
- Program for Decimal to Binary Conversion
- Python program to convert decimal to binary number
- Decimal to binary number using recursion
- Decimal representation of given binary string is divisible by 20 or not
- Decimal to Binary using recursion and without using power operator
- Binary to decimal and vice-versa in python
- Number of substrings with odd decimal value in a binary string
- Decimal representation of given binary string is divisible by 10 or not
- Decimal to binary conversion without using arithmetic operators
- Counting even decimal value substrings in a binary string
- Decimal representation of given binary string is divisible by 5 or not
- Queries for decimal values of subarrays of a binary array
- Recursive program to generate power set
- Recursive program to print all subsets with given sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



