BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java program for binary to decimal conversion

Last Updated: November 2, 2022 by Chaitanya Singh | Filed Under: Java Examples

There are two following ways to convert binary number to decimal number:

1) Using Integer.parseInt() method of Integer class.
2) Do conversion by writing your own logic without using any predefined methods.

Method 1: Binary to Decimal conversion using Integer.parseInt() method

The Integer.parseInt() method accepts two arguments, first argument is the string which you want to parse and the second argument is the radix. Here, we provided the radix as 2, because we are converting a binary number. The radix is the base of the number we are converting, for example: radix is 8 for octal number conversion, for hex to decimal radix 16 and so on.

import java.util.Scanner;
class BinaryToDecimal {
    public static void main(String args[]){
       Scanner input = new Scanner( System.in );
       System.out.print("Enter a binary number: ");
       String binaryString =input.nextLine();
       System.out.println("Output: "+Integer.parseInt(binaryString,2));
    }
}

Output:

Enter a binary number: 1101
Output: 13

Method 2: Conversion without using parseInt

Here, we are not using the parseInt() method. We have created a user defined method BinaryToDecimal(), where we have written the logic for binary to decimal conversion.

public class Details {
 
  public int BinaryToDecimal(int binaryNumber){
 
    int decimal = 0;
    int p = 0;
    while(true){
      if(binaryNumber == 0){
        break;
      } else {
          int temp = binaryNumber%10;
          decimal += temp*Math.pow(2, p);
          binaryNumber = binaryNumber/10;
          p++;
       }
    }
    return decimal;
  }
 
  public static void main(String args[]){
    Details obj = new Details();
    System.out.println("110 --> "+obj.BinaryToDecimal(110));
    System.out.println("1101 --> "+obj.BinaryToDecimal(1101));
    System.out.println("100 --> "+obj.BinaryToDecimal(100));
    System.out.println("110111 --> "+obj.BinaryToDecimal(110111));
  }
}

Output:

110 --> 6
1101 --> 13
100 --> 4
110111 --> 55

Recommended Posts

  • Java Octal to Decimal Conversion
  • Java Hexadecimal to Decimal Conversion
  • Java Decimal to Binary Conversion
❮ Java Programs

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program for Decimal to Octal Conversion
  3. Java program to get IP address
  4. Java Program to Add Two Complex Numbers
  5. Java Program to Find HCF and LCM of Two Numbers

Tags: Java-Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. rafikh says

    April 13, 2016 at 2:08 PM

    Please create notes for struts and other framework

    Reply
  2. jaga says

    July 21, 2016 at 12:08 PM

    I was about to know the Method 2 program .How to add or sum all the values that are displayed in console or command prompt
    eg:
    110 –> 6
    1101 –> 13
    100 –> 4
    110111 –> 55 i should get the decimal value at last

    Reply
  3. esousa says

    August 7, 2016 at 6:59 PM

    Thanks dude. This help-me a lot…..great job.

    Reply
  4. Dimitris says

    April 2, 2017 at 11:48 PM

    You need to cast (int) before Math.pow, as java returns a double() for this expression

    Reply
  5. Dimitris says

    April 2, 2017 at 11:51 PM

    Sorry , i was eager not to see the int temp inside the if. Well done mate :))

    Reply
  6. George says

    October 16, 2017 at 10:33 AM

    I wonder how to deal with negative powers in the input.
    Example if you want to convert the decimal 0.001 to binary.

    Reply

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap