Java Program to Convert Binary Number to Decimal and vice-versa

   
Java Program to Convert Binary Number to Decimal and vice-versa

What is a Binary Number?

A binary number is a number represented by a base-2 numeral system, which uses only two symbols: 0 and 1. Each digit, or bit, in a binary number represents a power of 2, with the rightmost bit representing 2^0 and the next bit to the left representing 2^1, and so on.

 

Convert a binary number to decimal :

  1. Start with the rightmost bit of the binary number and assign it the value 2^0.
  2. For each bit moving left, double the previous value and add the value of the current bit.

Here is an example of how to convert the binary number 1101 to decimal:

  1. The rightmost bit is 1, so 2^0 * 1 = 1.
  2. The next bit is 1, so 2^1 * 1 = 2.
  3. The next bit is 0, so 2^2 * 0 = 0.
  4. The leftmost bit is 1, so 2^3 * 1 = 8.
  5. Add up all the values (1 + 2 + 0 + 8) to get 11, which is the decimal equivalent of the binary number 1101.

Example 1:

Run

public class Main {
    public static void main(String[] args) {
        String binary = "1101";
        int decimal = binaryToDecimal(binary);
        System.out.println("Decimal equivalent of binary number " + binary + " is: " + decimal);
    }

    public static int binaryToDecimal(String binary) {
        int decimal = 0;
        int binaryLen = binary.length();
        for (int i = 0; i < binaryLen; i++) {
            int bit = binary.charAt(i) - '0';
            int power = binaryLen - i - 1;
            decimal += bit * Math.pow(2, power);
        }
        return decimal;
    }
}

Output :

Decimal equivalent of binary number 1101 is: 13

Convert decimal to binary :

  1. Divide the decimal number by 2.
  2. Write down the remainder.
  3. Repeat step 1 and 2 until the quotient is 0.
  4. Read the remainders from bottom to top.

Here is an example of how to convert the decimal number 13 to binary using the algorithm :

  1. Divide 13 by 2: quotient = 6, remainder = 1.
  2. Divide 6 by 2: quotient = 3, remainder = 0.
  3. Divide 3 by 2: quotient = 1, remainder = 1.
  4. Divide 1 by 2: quotient = 0, remainder = 1.
  5. Read the remainders from bottom to top: 1101.

Example 2:

Run

public class Main {
    public static void main(String[] args) {
        int decimal = 13;
        String binary = decimalToBinary(decimal);
        System.out.println("Binary equivalent of decimal number " + decimal + " is: " + binary);
    }

    public static String decimalToBinary(int decimal) {
        String binary = "";
        while (decimal > 0) {
            binary = (decimal % 2) + binary;
            decimal = decimal / 2;
        }
        return binary;
    }
}

Output :

Binary equivalent of decimal number 13 is: 1101

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription