In the previous article, we have discussed Java Program for Octal to Decimal
In this article we will discuss about how to convert Octal to Binary.
Program to convert octal to binary
Before jumping into the program directly, let’s first know about octal and binary.
Octal Number :
- The number system with base 8 is generally called octal number system .
- This number system us usually consists of 8 digits i.e., 0,1,2,3,4,5,6,7
- Example – (156)8 where “8” represents the base and “156” represents the octal.
- But (186)8 will be a wrong representation because the digits are possible between 0 to 7.
Binary Number :
- The number system with base 2 is generally called binary number system .
- This number system is usually consisting of 2 digits only i.e., 0 or 1 .
- Whenever there is a low voltage, it represents as 0 .
- Whenever there is a high voltage, it represents as 1 .
- Example – (10111)2 where “2” represent the base and “10111” represent the binary number.
When we convert the octal value to the binary value, we can convert by using 3-bit representation. Usually, to represent a digit in octal in binary we need at least 3 bit. Lets see how the 8 bits are represented.
Digit=0 , Then Binary representation=000 Digit=1 , Then Binary representation=001 Digit=2 , Then Binary representation=010 Digit=3 , Then Binary representation=011 Digit=4 , Then Binary representation=100 Digit=5 , Then Binary representation=101 Digit=6 , Then Binary representation=110 Digit=7 , Then Binary representation=111
Lets take a example to convert the octal to binary
(55)8 = For first 5 we represent 101 and For second 5 we represent 101
So combinedly we can write 101101
Which can also be written as (101101)2
Let’s see different ways to do it.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Method 1 : Java Program for Octal to Binary Using switch case
Approach :
- Take an octal value as the input.
- Take each possible value with the help of switch case .
- Combine it each time with a sting and Store it into a variable .
- Print the result .
Program:
Let’s see the program to understand it more clearly.
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner object Scanner sc = new Scanner(System.in); // input a octal value as a string through scanner class System.out.println("Enter a octal Value : "); String input1=sc.next(); int a=0; String output = ""; // iterating the complete length of octal string and assigning the equivalent binary value for each octal digit while (a < input1.length()) { // storing character according to the number of iteration char c = input1.charAt((int)a); // check all possible 8 conditions switch (c) { case '0': output += "000"; break; case '1': output += "001"; break; case '2': output += "010"; break; case '3': output += "011"; break; case '4': output += "100"; break; case '5': output += "101"; break; case '6': output += "110"; break; case '7': output += "111"; break; default: System.out.println("\nInvalid Octal Digit " + input1.charAt((int)a)); break; } a++; } System.out.println("Converted binary is :"+output); } }
Output : Enter a octal Value : 55 Converted binary is :101101
Method 2 : Java Program for Octal to Binary Using mathematical strategy
Approach :
- Take a octal value as the input .
- Take a for loop to iterate each digits of octal value and multiply with the power of 8 according to there position .
- Each time store it into a intermediate variable .
- Now convert the decimal value to its binary equivalent .
- Print the result .
Program :
Let’s see the program to understand it more clearly.
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner object Scanner sc = new Scanner(System.in); // input a octal value as a string through scanner class System.out.println("Enter a octal Value : "); int input1=sc.nextInt(); int i = 0; int decimal= 0; int output = 0; while (input1 != 0) { decimal += (input1 % 10) * Math.pow(8, i); ++i; input1 /= 10; } i = 1; // converting generated decimal number to its binary equivalent while (decimal!= 0) { output += (decimal % 2) * i; decimal /= 2; i *= 10; } System.out.println("Converted binary is :"+output); } }
Output : Enter a octal Value : 55 Converted binary is :101101
Method 3 : Java Program for Octal to Binary Using built in function
Approach :
- Take a octal value form the input .
- Convert it into its decmal value by using
Integer.parseInt(input value , 8)
and store it into a variable - Now again convert it using another buit in function
Integer.toBinaryString( )
Store it into a variable . - Print the result .
Program :
Let’s see the program to understand it more clearly.
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner object Scanner sc = new Scanner(System.in); // input a octal value as a string through scanner class System.out.println("Enter a octal Value : "); String input1=sc.next(); int octal = Integer.parseInt(input1, 8); String output = Integer.toBinaryString(octal); System.out.println("Converted binary is :"+output); } }
Output : Enter a octal Value : 55 Converted binary is :101101
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output
Related Java Programs: