Java Program to convert binary number to octal number

Here we are explaining how to write a java program to convert binary numbers to octal numbers.

What is the difference between a binary number and an octal number?

A binary number is a base two number(0,1). The octal number is base eight number. It includes 0 to 7.

Examples of binary number:11,00,101

Examples of hexadecimal number: 125,5645


How to implement the java program to convert binary numbers to octal numbers in java?

First, we have to declare the class BinToOct.Declare the integer variable bin,i,j,Set i=1.Declare the array oct[] to hold the octal number. Create an object of the scanner class as sc. Read the binary number from the user into the variable bin.Then by using while loop check if bin!=0,oct[i++]=bin%8,bin=bin/8.Display the octal number as the array oct.



STEP 1: Declare the class BinToOct with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3:Declare the integer variable bin,i,j and set i=1.

STEP 4:Declare the integer array oct[].

STEP 5: Create an object of the scanner class as sc.

STEP 6: Read the binary number from the user into the variable bin.

STEP 7: By using while loop checks if bin!=0, do step 8.

STEP 8: oct[i++]=bin mod 8,calculate bin=bin/8.

STEP 9: Display the octal number as the array oct.

Java Source Code

                                          import java.util.Scanner;

public class BinToOct{
 public static void main(String args[]){
 int bin,i=1, j;
 int oct[] = new int[100];
 Scanner sc = new Scanner(;  
 System.out.println("Enter the Binary Number : ");
 bin = sc.nextInt(); 
 while(bin != 0)
 oct[i++] = bin%8;
 bin = bin/8;
System.out.println("The Octal number is");
 for(j=i-1; j>0; j--)


Enter the Binary Number : 
The Octal number is