Java Program to Convert Octal to Binary Number

In this tutorial, we will write a java program to convert a octal number into binary using while loop. Before that, you must have knowledge of the following topics in C++.

Binary number

The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

Octal number

The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

Let us go through a program for the Ocatal to Binary Conversion in java.


Java Program to Convert Octal number to Binary Number

import java.util.Scanner;

public class Main
{
  public static void main(String args[])
  {
    int octalNum, rem, i = 0;
    int bin = 0, deciNum = 0;
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter a Octal Number: ");
    octalNum = sc.nextInt();

    while (octalNum != 0)
    {
      deciNum += (octalNum % 10) *Math.pow(8, i);
      ++i;
      octalNum /= 10;
    }

    i = 1;

    while (deciNum != 0)
    {
      bin += (deciNum % 2) *i;
      deciNum = deciNum / 2;
      i *= 10;
    }

    System.out.print("Equivalent Binary Value: " + bin);

  }
}

Output:

Enter a Octal Number: 12
Equivalent Binary Value: 1010