binary to decimal recursion java code example

Example: Convert binary to decimal in java using recursion

// Convert binary to decimal in java using recursion
public class BinaryToDecimalRecursion
{
   static int binaryToDecimal(String strBinary, int a) 
   {
      int size = strBinary.length(); 
      if(a == size - 1)
         return strBinary.charAt(a) - '0'; 
      return((strBinary.charAt(a) - '0') << (size - a - 1)) + binaryToDecimal(strBinary, a + 1); 
   }
   public static void main(String[] args) 
   {
      String binaryNumber = "1111"; 
      int n = 0;
      System.out.println("Convert binary to decimal using recursion: ");
      System.out.println(binaryToDecimal(binaryNumber, n));
   }
}

Tags:

Java Example