JAVA Program to Convert Octal to Binary


Octal Number − Octal number is also one of the number systems available. The octal number is represented with 8 digits which is from 0 to 7(0, 1, 2, 3... 7). The Octal numbers are expressed as base-8 in the numeral system.

Binary Number − There are four types of number systems available. Binary numbers are one of them. The Binary number is basically represented with two digits i.e. one (1) and zero (0). The binary numbers are expressed as base-2 in the numeral system.

Here we have a set of data in which we already store the binary value of octal number digits (0 to 7). So, after getting any octal number to convert, we first divide it into parts then compare those digits with the data set and choose the binary numbers. After that we concatenate those all-binary values. By this method we are converting the octal number into a Binary number.

In this article we will see how to convert octal to binary in Java.

To show you some instances −

Instance-1

Input octal number is 235.
The binary of 2 = 010.
The binary of 3 = 011.
The binary of 5 = 101.
So the binary of 235 = 010011101.

Instance-2

Input octal number is 45.
The binary of 4 = 100.
The binary of 5 = 101.
So binary of 45 = 100101.

Instance-3

Input octal number is 1234.
The binary of 1 = 001.
The binary of 2= 010.
The binary of 3 = 011.
The binary of 4 = 100.
So the binary of 235 = 001010011100.

Algorithm

Step-1 − Get the input number either by static input or user input method.

Step-2 − Inside the user defined method we declared some switch cases in which we store the respective binary values of octal number digits from 0 to 7.

Step-3 − We declare a loop to extract digits from the whole number and pass those digits into the switch case.

Step-4 − After getting the binary values of the digits we concatenate the value as per the arrangement of digits like input number.

Step-5 − At the end we print the Binary number as output.

Multiple Approaches

We have provided the solution in different approaches.

  • By User Defined Method with Static Input Values.

  • By User Defined Method with User Input Values.

Let’s see the program along with its output one by one.

Approach-1: By Using User Defined Method with Static Input Value

In this approach, we declare an octal input number by static input method and pass this number as a parameter in a user defined method, then inside the method by using the algorithm we can convert the octal number into binary number.

Example

public class Main {
   public static void main(String args[]){
      String inputNumber = "432";
      System.out.println("Given Octal number: " + inputNumber);
      String res = octalToBinary(inputNumber);
      System.out.println("The Binary number of given octal number is: "+ res);
   }
   static String octalToBinary(String octalNum){
      int i = 0;
      String binary = "";
      while (i < octalNum.length()) {
         char c = octalNum.charAt((int)i);
         switch (c) {
            case '0':
            binary += "000";
            break;
            
            case '1':
            binary += "001";
            break;
            
            case '2':
            binary += "010";
            break;
 
            case '3':
            binary += "011";
            break;
 
            case '4':
            binary += "100";
            break;
            
            case '5':
            binary += "101";
            break;
 
            case '6':
            binary += "110";
            break;
 
            case '7':
            binary += "111";
            break;
            default:
            System.out.println( "\nYou provide an invalid Octal Digit- " + octalNum.charAt((int)i));
            break;
         }
         i++;
      }
   return binary;
   }
}

Output

Given Octal number: 432
The Binary number of given octal number is: 100011010

Approach-2: By Using User Defined Method with User Input Value

In this approach, we declare an octal input number by user input method and pass this number as a parameter in a user defined method, then inside the method by using the algorithm we can convert the octal number into binary number.

Example

import java.util.Scanner;
public class Main {
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter an Octal number: ");
      String inputNumber = sc.nextLine();
      String res = octalToBinary(inputNumber);
      System.out.println("The Binary number of given octal number is: " + res);
   }
   static String octalToBinary(String octalNum) {
      int i = 0;
      String binary = "";
      while (i < octalNum.length()) {
         char c = octalNum.charAt((int)i);
         switch (c) {
            case '0':
            binary += "000";
            break;
 
            case '1':
            binary += "001";
            break;
            
            case '2':
            binary += "010";
            break;
 
            case '3':
            binary += "011";
            break;
 
            case '4':
            binary += "100";
            break;
            
            case '5':
            binary += "101";
            break;
            
            case '6':
            binary += "110";
            break;
 
            case '7':
            binary += "111";
            break;
            default:
            System.out.println("\nYou provide an invalid Octal Digit- " + octalNum.charAt((int)i));
            break;
         }
         i++;
      }
      return binary;
   }
}

Output

Enter an Octal number: 2345
The Binary number of given octal number is: 010011100101

In this article, we explored how to convert octal to binary in Java by using different approaches.

Updated on: 27-Dec-2022

846 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements