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

   
Java Program to Convert Binary Number to Octal 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 binary number to octal number :

  1. Divide the binary number into groups of three, starting from the rightmost digit.
  2. Convert each group of three binary digits into its corresponding octal digit.
  3. Write the octal digits from left to right to get the final octal number.

Procedure : Convert the binary number “110101” to octal.

  1. Divide the binary number into groups of three: 110 101
  2. Convert each group of three binary digits into its corresponding octal digit: 6 5
  3. Write the octal digits from left to right to get the final octal number: 65

In this example, the binary number “110101” is equivalent to the octal number “65“.

Example 1:

Run

public class Main {
    public static String binaryToOctal(String binary) {
        String octal = "";
        while (binary.length() % 3 != 0) {
            binary = "0" + binary;
        }
        for (int i = 0; i < binary.length(); i += 3) {
            int decimal = Integer.parseInt(binary.substring(i, i + 3), 2);
            octal += Integer.toString(decimal);
        }
        return octal;
    }

    public static void main(String[] args) {
        String binaryNumber = "11010101";
        String octalNumber = binaryToOctal(binaryNumber);
        System.out.println("Binary number: " + binaryNumber);
        System.out.println("Octal equivalent: " + octalNumber);
    }
}

Output :

Binary number: 11010101
Octal equivalent: 325

Convert octal number into binary number :

  1. Write down the octal number to be converted.
  2. Break the octal number into individual digits.
  3. Convert each digit of the octal number to its binary equivalent by finding the corresponding 3-digit binary number.
  4. Concatenate all the binary numbers.

Procedure: Convert the octal number “65” to binary.

  1. Write down the octal number: 65
  2. Break the octal number into individual digits: 6, 5
  3. Convert each digit of the octal number to its binary equivalent: 110 101
  4. Concatenate all the binary numbers: 110101

In this example, the octal number “65” is equivalent to the binary number “110101“.

Example 2:

Run

public class Main {
    public static String octalToBinary(String octal) {
        String binary = "";
        for (int i = 0; i < octal.length(); i++) {
            int decimal = Integer.parseInt(Character.toString(octal.charAt(i)));
            binary += Integer.toBinaryString(decimal);
        }
        return binary;
    }

    public static void main(String[] args) {
        String octalNumber = "65";
        String binaryNumber = octalToBinary(octalNumber);
        System.out.println("Octal number: " + octalNumber);
        System.out.println("Binary equivalent: " + binaryNumber);
    }
}

Output :

Octal number: 65
Binary equivalent: 110101

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