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 :
- Divide the binary number into groups of three, starting from the rightmost digit.
- Convert each group of three binary digits into its corresponding octal digit.
- Write the octal digits from left to right to get the final octal number.
Procedure : Convert the binary number “110101” to octal.
- Divide the binary number into groups of three: 110 101
- Convert each group of three binary digits into its corresponding octal digit: 6 5
- 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:
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
Explanation :
The function takes a binary number as a string, converts it to an octal number and returns it as a string. And in the main class, you can test this function by passing different binary numbers to the function and check the output.
Convert octal number into binary number :
- Write down the octal number to be converted.
- Break the octal number into individual digits.
- Convert each digit of the octal number to its binary equivalent by finding the corresponding 3-digit binary number.
- Concatenate all the binary numbers.
Procedure: Convert the octal number “65” to binary.
- Write down the octal number: 65
- Break the octal number into individual digits: 6, 5
- Convert each digit of the octal number to its binary equivalent: 110 101
- Concatenate all the binary numbers: 110101
In this example, the octal number “65” is equivalent to the binary number “110101“.
Example 2:
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
Explanation :
The function takes an octal number as a string, converts it to a binary number and returns it as a string. And in the main class, you can test this function by passing different octal numbers to the function and check the output.
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
Login/Signup to comment