w3resource

Java: Convert a octal number to a binary number

Java Basic: Exercise-26 with Solution

Write a Java program to convert a octal number to a binary number.

Octal number: The octal numeral system is the base-8 number system, and uses the digits 0 to 7.

Binary number: A binary number is a number expressed in the base-2 numeral system or binary numeral system. This system uses only two symbols: typically 1 (one) and 0 (zero).

Test Data:
Input any octal number: 7

Pictorial Presentation: Octal to Binary number

Java: Convert a octal number to a binary number

Sample Solution:

Java Code:

import java.util.Scanner;

public class Exercise26 {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner in = new Scanner(System.in);
        
        // Define an array to map octal digits to their binary equivalents
        int[] octal_numvalues = {0, 1, 10, 11, 100, 101, 110, 111};
        
        // Declare variables to store octal, temporary octal, and binary numbers, and a place value
        long octal_num, tempoctal_num, binary_num, place;
        int rem;
        
        // Prompt the user to input an octal number
        System.out.print("Input any octal number: ");
        octal_num = in.nextLong();
        tempoctal_num = octal_num;
        binary_num = 0;
        place = 1;
        
        // Convert the octal number to binary using the mapping array
        while (tempoctal_num != 0) {
            rem = (int)(tempoctal_num % 10);
            binary_num = octal_numvalues[rem] * place + binary_num;
            tempoctal_num /= 10;
            place *= 1000;
        }
        
        // Display the equivalent binary number
        System.out.print("Equivalent binary number: " + binary_num + "\n");
    }
}

Explanation:

In the exercise above -

  • First, it takes an octal number as input from the user using the "Scanner" class and stores it in the variable 'octal_num'.
  • It initializes an array 'octal_numvalues' to store the binary equivalents of octal digits (0 to 7).
  • It initializes variables 'tempoctal_num' to temporarily store the octal number, 'binary_num' to store the binary equivalent, 'place' to keep track of the position of each octal digit, and 'rem' to store the remainder when dividing 'tempoctal_num' by 10.
  • Next it enters a loop to perform the octal-to-binary conversion:
    • In each iteration, it calculates the remainder 'rem' when 'tempoctal_num' is divided by 10 (which gives the least significant octal digit).
    • It uses 'rem' to look up the binary equivalent from the 'octal_numvalues' array and adds it to 'binary_num', taking into account the appropriate place value.
    • It divides 'tempoctal_num' by 10 to remove the least significant digit (rightmost).
    • It updates the 'place' variable to move to the next group of three binary digits.
    • The loop continues until 'tempoctal_num' becomes zero, effectively converting the entire octal number to binary.
  • Finally (after the loop), it prints the binary representation of the original octal number stored in the 'binary_num' variable.

Sample Output:

Input any octal number: 7                                                                                    
Equivalent binary number: 111

Flowchart:

Flowchart: Java exercises: Convert a octal number to a binary number

Java Code Editor:

Previous: Write a Java program to convert a octal number to a decimal number.
Next: Write a Java program to convert a octal number to a hexadecimal number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.