Java Program: Convert Binary Number to Octal and Vice-Versa
In this blog post, we will delve into a Java program designed to convert a binary number to octal and vice-versa. The article will provide a comprehensive explanation of the algorithm, present the Java code for implementation, and include examples with corresponding outputs.
Understanding the Algorithm
The algorithm for converting between binary and octal numbers involves the following steps:
Binary to Octal Conversion:
- Input Binary Number: Receive the binary number as input.
- Grouping into Sets of Three: Group the binary digits into sets of three, starting from the right.
- Conversion to Octal: Convert each set of three binary digits into its octal equivalent.
- Combine Octal Digits: Combine the octal equivalents to obtain the final octal number.
Octal to Binary Conversion:
- Input Octal Number: Receive the octal number as input.
- Convert Each Octal Digit: Convert each octal digit into its binary equivalent.
- Combine Binary Digits: Combine the binary equivalents to obtain the final binary number.
Java Program for Binary to Octal and Octal to Binary Conversion
Let’s implement the algorithm in a Java program:
import java.util.Scanner;
public class BinaryOctalConverter {
// Function to convert binary to octal
static String binaryToOctal(String binary) {
// Group binary digits into sets of three from the right
while (binary.length() % 3 != 0) {
binary = '0' + binary;
}
StringBuilder octal = new StringBuilder();
// Convert each set of three binary digits to octal
for (int i = 0; i < binary.length(); i += 3) {
String triplet = binary.substring(i, i + 3);
int decimalEquivalent = Integer.parseInt(triplet, 2);
octal.append(Integer.toOctalString(decimalEquivalent));
}
return octal.toString();
}
// Function to convert octal to binary
static String octalToBinary(String octal) {
StringBuilder binary = new StringBuilder();
// Convert each octal digit to binary
for (int i = 0; i < octal.length(); i++) {
char octalDigit = octal.charAt(i);
String binaryEquivalent = Integer.toBinaryString(Integer.parseInt(String.valueOf(octalDigit), 8));
// Ensure each binary equivalent has three digits
while (binaryEquivalent.length() < 3) {
binaryEquivalent = '0' + binaryEquivalent;
}
binary.append(binaryEquivalent);
}
return binary.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Binary to Octal conversion
System.out.print("Enter binary number: ");
String binaryNumber = scanner.next();
String octalResult = binaryToOctal(binaryNumber);
System.out.println("Octal equivalent: " + octalResult);
// Octal to Binary conversion
System.out.print("Enter octal number: ");
String octalNumber = scanner.next();
String binaryResult = octalToBinary(octalNumber);
System.out.println("Binary equivalent: " + binaryResult);
}
}
Output Example
Example: Binary to Octal and Octal to Binary Conversion
Consider the following input:
Binary to Octal
Enter binary number: 1101101
Octal equivalent: 155
Octal to Binary
Enter octal number: 34
Binary equivalent: 011100
Explanation
The Java program defines two functions, binaryToOctal
and octalToBinary
, for converting between binary and octal representations. The main
method prompts the user for input, performs the conversions, and displays the results.
Conclusion
This Java program provides a flexible and reusable solution for converting binary numbers to octal and vice-versa. You can use this program as a standalone utility or incorporate it into larger Java projects. Feel free to experiment with different input values and explore variations of the conversion process. If you have any questions or would like to explore more Java programming topics, don’t hesitate to ask!