Program to Convert Binary to Decimal in Java

Here you will get program to convert binary to decimal in Java.

There are mainly two ways to convert a binary number to decimal number in Java.

1. By using parseInt() method of Integer class.
2. By using user defined logic.

Program to Convert Binary to Decimal in Java

By using Integer.parseInt()

Integer.parseInt() method takes two arguments. First argument is a string and second argument is the base or radix in which we have to convert the number. The output is the integer represented by the string argument in the specified radix. Below is the program for it.



Without using Integer.parseInt()

In this method we have to define our own logic for converting binary number to decimal. The approach that we will use here is mentioned in below example.

binary to decimal example

Image Source

The program that implements above approach is mentioned below.




Program to Convert Binary to Decimal in Java
If you found anything missing or incorrect in above programs then please mention it by commenting below.

16 thoughts on “Program to Convert Binary to Decimal in Java

  1. Bismeet Singh

    import java.util.Scanner;
    public class Binary2Decimal{
    public static void main(String[] args) {
    Scanner no=new Scanner(;
    System.out.println(“Enter a binary number”);
    String n=no.nextLine();
    System.out.println(Integer.parseInt(n, 2));
    Exception in thread “main” java.lang.NumberFormatException: For input string: “2”
    at java.lang.NumberFormatException.forInputString(
    at java.lang.Integer.parseInt(
    at binary2decimal.Binary2Decimal.main(

    1. Admin Post author

      You have to enter binary number, here you have entered decimal number 2 so exception is coming.

      1. Nikhil Kulkarni

        y dont you restrict users to give binary number input format only

  2. Ab niazi

    public class Bin2dec {

    * @param args the command line arguments
    public static void main(String[] args) {
    Scanner s = new Scanner(;
    System.out.println(“Enter the Word”);
    String str =;
    int number;
    double digit = 0 , result = 0;
    int check = 1;
    for(int i = 0; i < str.length() ; i++){
    if(str.charAt(i) =='1' || str.charAt(i) =='0'){
    check = 1;
    check = 0;
    if(check == 0){
    System.out.println("Error: Invalid Binary String \"" + str + "\"");
    else if(check == 1){
    number = Integer.parseInt(str);
    for(int i = 0; i < str.length() ; i++){
    digit = number % 10;
    number = number / 10;
    result = result + (digit * pow(2,i));

  3. Habib

    Given code is wrong for input 2100
    You just need to use break statement after check=0

  4. Palak Davda

    class BinaryToDecimal
    public static void main(String [] args) throws IOException
    BufferedReader br = new BufferedReader (new InputStreamReader (;
    System.out.println(“Enter a binary a number:”);
    String a = br.readLine();
    char x[] = a.toCharArray();
    int power=0;
    long decimal=0 , sum=0;
    for(int i =x.length-1 ; i>=0;i–)

    int z = (int)java.lang.Math.pow(2,power);
    sum = sum + ((x[i])*(z));

  5. pradeep

    hi sir,
    please describe this line


  6. Vontu

    Hi, the program above is correct but the example image is not correct.

  7. winson

    I’m a beginner, may I ask about the purpose of n=n/10 ?

    1. Neeraj Mishra Post author

      When we divide any number with 10 the last digit of number is removed from result. For example 123/10 = 12, we have to extract digits from last one be one thats why we used n%10 and n/10.

  8. koshika Gupta

    How to convert binary to Hexadecimal number using inbuit functions:


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.