Site icon

Program to convert binary number to decimal (java api, iterative & recursive)

Let us look into different methods to convert binary number to decimal number in java.

Method 1: Convert binary number to decimal in java (Integer.parseInt)

//binaryNumber = "1010"
private static int toDecimalUsingParseInt(String binaryNumber) {
        //output: decimal = 10
	return Integer.parseInt(binaryNumber, 2);
}

Method 2: Convert binary number to decimal in java (iterative method)

//binaryNumber = "1010"
private static int iterative(String binaryNumber) {
	int power = 0;
	int decimal = 0;
	for (int index = 0; index < binaryNumber.length(); index++) {
		char chValue = binaryNumber.charAt(binaryNumber.length() - index - 1);
		int digit = Character.getNumericValue(chValue);
		decimal = decimal + digit * (int) Math.pow(2, power++);
	}
	return decimal;
} //output: decimal = 10

Method 3: Convert binary number to decimal in java (recursive method)

//binaryNumber = "1010"
private static int recursive(String binaryNumber) {
	int decimal = 0;
	int length = binaryNumber.length();
	if (length > 0) {
		String substring = binaryNumber.substring(1);
		int digit = Character.getNumericValue(binaryNumber.charAt(0));
		decimal = digit * (int) Math.pow(2, length - 1) + recursive(substring);
	}
	return decimal;
}//output: decimal = 10

The logical flow of recursive method for input binary string “1010” is shown in Fig 1.

Fig 1: Binary String “1010” to decimal number

Program: convert binary number to decimal (java, iterative & recursive)

package org.learn;

import java.util.Scanner;

public class BinaryToDecimal {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			
			System.out.printf("1. Enter binary string : ");
			String binary = scanner.next();

			int decimal = toDecimalUsingParseInt(binary);
			System.out.printf("2. Binary to decimal - parseInt(%s,2) = %d ", binary, decimal);

			decimal = iterative(binary);
			System.out.printf("\n3. Binary to decimal - iterative(%s) = %d ", binary, decimal);

			decimal = recursive(binary);
			System.out.printf("\n4. Binary to decimal - recursive(%s) = %d ", binary, decimal);
		}
	}

	private static int toDecimalUsingParseInt(String binaryNumber) {
		return Integer.parseInt(binaryNumber, 2);
	}

	private static int iterative(String binaryNumber) {
		int power = 0;
		int decimal = 0;
		for (int index = 0; index < binaryNumber.length(); index++) {
			char chValue = binaryNumber.charAt(binaryNumber.length() - index - 1);
			int digit = Character.getNumericValue(chValue);
			decimal = decimal + digit * (int) Math.pow(2, power++);
		}
		return decimal;
	}

	private static int recursive(String binaryNumber) {
		int decimal = 0;
		int length = binaryNumber.length();
		if (length > 0) {
			String substring = binaryNumber.substring(1);
			int digit = Character.getNumericValue(binaryNumber.charAt(0));
			decimal = digit * (int) Math.pow(2, length - 1) + recursive(substring);
		}
		return decimal;
	}
}

Output: convert binary number to decimal (java, iterative & recursive)

1. Enter binary string : 1010
2. Binary to decimal - parseInt(1010,2) = 10 
3. Binary to decimal - iterative(1010) = 10 
4. Binary to decimal - recursive(1010) = 10 

1. Enter binary string : 11100110
2. Binary to decimal - parseInt(11100110,2) = 230 
3. Binary to decimal - iterative(11100110) = 230 
4. Binary to decimal - recursive(11100110) = 230 
Exit mobile version