In the previous article, we have discussed Java Program for Binary to Decimal
In this article we will discuss about how to convert Binary to Octal.
Java Programs to Convert Binary to Octal
Before jumping into the program directly, let’s first know about binary and octal.
Binary:
Binary number mainly consists of only two numbers i.e. 0 and 1. The base address of the binary number is 2. For low voltage signal the value will 0 and for the high voltage signal the value will 1.
Example: (1001)2, (111000)2
where, (number)2, number represents the number and 82represents the base.
Octal:
Octal number basically defines the base of 8 in the number system. The number is specially consists of 0,1,2,3,4,5,6 and 7 so this number requires 3 bit to represent this octal number.
Example: (534)8, (26)8
where, (number)8, number represents the number and 8 represents the base.
Let’s see different ways to convert binary to octal.
Method-1: Java Program for Binary to Octal By using toOctalString() method
In this approach, first we will convert binary number to an integer. After that by using toOctalString()
method the java converts the string into octal numbers and integer again. Let’s see the program to understand it more clearly.
import java.util.*; public class BinaryToOctal { //conversion of binary to decimal int binaryToDecimal(long binary_no) { // variable to store the decimal number int decimal_Number = 0, k = 0; while (binary_no > 0) { decimal_Number+= Math.pow(2, k++) * (binary_no % 10); // dividing the binary by 10 binary_no /= 10; } // for returning the converted decimal number return decimal_Number; } // function to convert decimal to octal int decimalToOctal(long binary_no) { // variable to store the decimal number // returned by the binaryToDecimal() int decimal_Number = binaryToDecimal(binary_no); // using the toOctalString() to convert String octalString= Integer.toOctalString(decimal_Number); // converting the String of octal number int octal_Number = Integer.parseInt(octalString); // returning the octal number return octal_Number; } //driver method public static void main(String[] args) { // for instantiating the class BinaryToOctal ob = new BinaryToOctal(); Scanner sc = new Scanner(System.in); System.out.println("Enter the binary number : "); long n = sc.nextLong(); System.out.println("After converting the "+n+" is : "+ob.decimalToOctal(n)); } }
Output: Enter the binary number : 101111111 after converting the 101111111 is : 577
Method-2: Java Program for Binary to Octal By extracting the remainder dividing by 8
In this approach first conversion takes place from binary to decimal. Then in a continuous manner the decimal number converts into octal by extracting remainder and dividing by number 8. Let’s see the program to understand it more clearly.
import java.util.*; public class BinaryToOctal { // conversion of binary number int binaryToDecimal(long binary_no) { //for variable declaration to store int decimal_Number = 0, k = 0; while (binary_no > 0) { // extracting every digit of the decimal_Number+= Math.pow(2, k++) * (binary_no % 10); // dividing the number by 10 // to remove the last digit binary_no /= 10; } // returning the converted decimal return decimal_Number; } int decimalToOctal(long binary_no) { // variable to store the octal number int octal_Number = 0, k = 0; // variable to store the output int decimal_Number = binaryToDecimal(binary_no); // loop to convert decimal to octal while (decimal_Number != 0) { // extracting the remainder on octal_Number += (decimal_Number % 8)* ((int)Math.pow(10, k++)); // removing the last digit by decimal_Number /= 8; } // returning the converted octal number return octal_Number; } public static void main(String[] args) { // for instantiating the class BinaryToOctal ob = new BinaryToOctal(); Scanner sc = new Scanner(System.in); System.out.println("Enter the binary number : "); long n = sc.nextLong(); System.out.println("After converting the "+n+" is : "+ob.decimalToOctal(n)); } }
Output: Enter the binary number : 1111 After converting the 1111 is: 17
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs: