Java program to convert binary number to octal and vice versa

www.‮aeditfigi‬.com

Here's a Java program to convert a binary number to octal and vice-versa:

import java.util.Scanner;

public class BinaryOctalConverter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a binary number:");
        String binaryString = scanner.nextLine();

        String octalValue = binaryToOctal(binaryString);
        System.out.println("The octal equivalent of " + binaryString + " is " + octalValue);

        System.out.println("Enter an octal number:");
        String octalString = scanner.nextLine();

        String binaryValue = octalToBinary(octalString);
        System.out.println("The binary equivalent of " + octalString + " is " + binaryValue);
    }

    public static String binaryToOctal(String binaryString) {
        String octalValue = "";
        int binaryLength = binaryString.length();
        int padding = (3 - (binaryLength % 3)) % 3;
        binaryString = "0".repeat(padding) + binaryString;
        binaryLength = binaryString.length();
        for (int i = 0; i < binaryLength; i += 3) {
            String binaryChunk = binaryString.substring(i, i + 3);
            int decimalValue = binaryToDecimal(binaryChunk);
            octalValue += decimalToOctal(decimalValue);
        }
        return octalValue;
    }

    public static String octalToBinary(String octalString) {
        String binaryValue = "";
        int octalLength = octalString.length();
        for (int i = 0; i < octalLength; i++) {
            int decimalValue = Character.getNumericValue(octalString.charAt(i));
            String binaryChunk = decimalToBinary(decimalValue);
            int padding = (i == 0) ? 0 : 3 - binaryChunk.length();
            binaryValue += "0".repeat(padding) + binaryChunk;
        }
        return binaryValue;
    }

    public static int binaryToDecimal(String binaryString) {
        int decimalNumber = 0;
        int base = 1;
        for (int i = binaryString.length() - 1; i >= 0; i--) {
            if (binaryString.charAt(i) == '1') {
                decimalNumber += base;
            }
            base *= 2;
        }
        return decimalNumber;
    }

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

    public static String decimalToOctal(int decimalValue) {
        String octalValue = "";
        while (decimalValue > 0) {
            octalValue = (decimalValue % 8) + octalValue;
            decimalValue /= 8;
        }
        return octalValue;
    }
}

In this program, we first prompt the user to enter a binary number using the Scanner class. We then call a method called binaryToOctal to convert the binary number to an octal value, and print the result to the console. We then prompt the user to enter an octal number, and call a method called octalToBinary to convert the octal value to a binary value, and print the result to the console.

The binaryToOctal method takes a binary string as input, and pads the string with leading zeros to ensure that its length is a multiple of 3. It then splits the string into chunks of