Programs

Java Program to Convert Binary to Decimal

The Binary number system is a base 2 system that consists of 0’s and 1’s. The binary numbers are well understood by computers and digital systems, hence termed as ‘machine language’. Each digit in a binary system holds the place value of the power of 2.

The decimal number system is identified by humans. It is a Base 10 numbering system, using digits from 0 to 9. Each digit in the decimal holds a place value of power of 10.

Let’s discuss how to convert binary to decimal in Java in this article. 

Importance of Binary to Decimal Conversion

The complex binary language needs conversion to a human-understandable format before it reaches the common users. Hence, we require calculation methods on how to convert binary to decimal and easily figure out what they signify.

Check out our free technology courses to get an edge over the competition.

Mathematical calculation for converting binary to Decimal

Since binary numbers are base 2 numbering systems, every place holds the value 2n. For instance, the first place has a value 20 = 1, the second place has a value 21 = 2, the third place 22 = 4 and so on. 

Let’s consider an example:

Convert Binary number 101101 to decimal number:

1 0 1 1 0 1

MSB                                                                                                          LSB

The number on the leftmost side is called MSB or Most Significant bit with a maximum power of 2, and the number on the Rightmost side is called LSB or Least Significant bit, with minimum power of 2.

Now, let’s see the mathematical binary decimal converter method for the following.

(101101)2

=1 x 25 + 0 x 24 + 1x 23 + 1x 22 + 0 x 21 + 1 x 20

= 32 +0 + 8 + 4 + 0+ 1

= (45)10

It seems interesting for a small binary number. But it is a difficult task to be considered while the number is really long. In such situations, we can’t depend on manual calculations, and we require a binary decimal converter.

Check Out upGrad’s Software Development Courses

Java Program to convert Binary to Decimal

Coding has its own importance in the digital era. It helps to replace the manual burden of decoding the machine language into a human-readable form. It enables a quick and efficient way of converting any binary number into a decimal number with no tedious efforts.

Let’s learn how to convert binary to decimal in Java. There are two possible methods to write a Java program to convert binary to decimal.

  1. Using ParseInt () method of class Integer
  2. User defined or custom logic method

Explore our Popular Software Engineering Courses

  • Using ParseInt () Method

In the program, two arguments are generally used. The first argument is String and the second one is the base required to convert the number. The output will be an integer that is represented by the string argument in the mentioned base.

There are two possible ways of using ParseInt () to convert binary to decimal in Java.

  • By prompting the user to enter the binary number:

Input:

Output:

Reference image: URL_beginnersbook_2014/07

In the above program, we make use of the Scanner class. Usually, we use the Scanner class in case the user input is required in the program. The Scanner class is found in the Java.util package. 

In-Demand Software Development Skills

Hence, the user is asked to provide a binary number to be converted to decimal as per the choice, and the converted output is obtained for the number provided.

  • By including the binary number in the program itself:

Input:

Output:

Reference Image: Reference_Javatpoint

In the above program, the binary numbers are included and converted decimal output is obtained for the input binary numbers.

  • User Defined logic/ Custom Logic

Input:

Output:

Reference Image: Reference_Javatpoint

The above Java program doesn’t use the ParseInt function. The program is written with user defined inputs. 

Binary numbers are most preferred and understood by modern digital and electronic machines, whereas decimal numbers are perceived by the general audience. Hence, both are necessary in the current world. 

Read our Popular Articles related to Software Development

The quick and effortless conversion of binary to decimal in Java saves the time and reduces the burden of calculating it manually.

At upGrad, we understand the importance of practical, hands-on learning – especially when it comes to software development. As a result, our courses and training initiatives have practicality at their very core. One such initiative is Full Stack Development Bootcamp which will help you develop all the relevant skills required to excel in full-stack development. 

Want to share this article?

Plan your Software Development Career Now!

Leave a comment

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

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

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

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks