## Binary To Decimal Conversion Java Program

By | 2018-03-21T21:11:41+00:00 June 18th, 2016|java programs|0 Comments

Here we are going to write a program on conversion of Binary to Decimal Numbers in Java.

## Binary To Decimal Conversion :

```import java.util.Scanner;

public class ConvertBinaryToDecimal {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Binary Number : ");
String input = sc.nextLine();
convertBinaryToDecimal(input);
}

public static int convertBinaryToDecimal(String number) {
int length = number.length() - 1;
int decimal = 0;
if (isBinary(number)) {
char[] digits = number.toCharArray();
for (char digit : digits) {
if (String.valueOf(digit).equals("1")) {
decimal += Math.pow(2, length);
}
--length;
}
System.out.println("The Decimal Number is : " + decimal);
}
return decimal;
}

public static boolean isBinary(String number) {

boolean isBinary = false;

if (number != null && !number.isEmpty()) {
int num = Integer.parseInt(number);
while (num > 0) {
if (num % 10 <= 1) {
isBinary = true;
} else {
isBinary = false;
break;
}
num /= 10;
}
}

return isBinary;
}

}```

Output :

```Enter a Binary Number : 1011101
The decimal number is : 93```

Happy Learning ðŸ™‚