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

As a Java programmer, one common task you may encounter is the need to convert binary numbers to decimal and vice-versa.

Binary numbers are numbers that are represented in the base-2 numeral system, while decimal numbers are represented in the base-10 numeral system.

In this tutorial, we’ll explore how to convert binary numbers to decimal and decimal numbers to binary using Java.


Converting Binary Numbers to Decimal

To convert a binary number to decimal, you can use the following steps:

  1. Initialize a variable to store the decimal value, let’s call it ‘decimal’.
  2. Get the binary number as input from the user or from a source.
  3. Iterate through each digit of the binary number, starting from the rightmost digit.
  4. For each digit, multiply it by 2 raised to the power of its position in the binary number, starting from 0 for the rightmost digit.
  5. Add the result of step 4 to the ‘decimal’ variable.
  6. Repeat steps 3-5 for all the digits in the binary number.
  7. Output the ‘decimal’ variable as the decimal equivalent of the binary number.

Here’s a Java program that implements the above algorithm:

import java.util.Scanner;

public class BinaryToDecimal {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a binary number: ");
        String binary = scanner.nextLine();
        int decimal = 0;
        int power = 0;
        for (int i = binary.length() - 1; i >= 0; i--) {
            int digit = Character.getNumericValue(binary.charAt(i));
            decimal += digit * Math.pow(2, power);
            power++;
        }
        System.out.println("Decimal equivalent: " + decimal);
    }
}

Converting Decimal Numbers to Binary

To convert a decimal number to binary, you can use the following steps:

  1. Initialize a variable to store the binary value, let’s call it ‘binary’.
  2. Get the decimal number as input from the user or from a source.
  3. Initialize a variable to store the remainder of the decimal number divided by 2, let’s call it ‘remainder’.
  4. Divide the decimal number by 2 and store the result in the decimal number variable.
  5. Append the value of the ‘remainder’ variable to the ‘binary’ variable.
  6. Repeat steps 3-5 until the decimal number becomes 0.
  7. Reverse the order of the digits in the ‘binary’ variable.
  8. Output the ‘binary’ variable as the binary equivalent of the decimal number.

Here’s a Java program that implements the above algorithm:

import java.util.Scanner;

public class DecimalToBinary {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a decimal number: ");
        int decimal = scanner.nextInt();
        String binary = "";
        while (decimal != 0) {
            int remainder = decimal % 2;
            binary = remainder + binary;
            decimal /= 2;
        }
        System.out.println("Binary equivalent: " + binary);
    }
}

Conclusion

Converting binary numbers to decimal and decimal numbers to binary is a common task in programming.

In this tutorial, we’ve explored how to convert binary numbers to decimal and decimal numbers to binary using Java.

By following the simple algorithms outlined above, you should be able to implement these conversions in your Java programs.