Recursive Program for Binary to Decimal - GeeksforGeeks

28 May, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    string binary = "1010";    cout << toDecimal(binary) << endl;    return 0;}
// 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)
# 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 codeif __name__ == "__main__" :         binary = "1010"    print(toDecimal(binary)) # This code is contributed by Ryuga
// 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// 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?>
<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>
10
 
ankthon
ukasp
ihritik
avanitrachhadiya2155
base-conversion
binary-string
C++ Programs
Recursion
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Program to implement Singly Linked List in C++ using class
Const keyword in C++
cout in C++
Dynamic _Cast in C++
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction