Java convert decimal to octal – Vice versa

Let’s learn java convert decimal to octal.

Java convert decimal to octal

Write program to convert given decimal number to equivalent octal number is one of the frequently asked interview question.

In the below java program we need to convert given decimal number (base value 10) into octal number (base value 8).

java convert decimal to octal

To represent numeric value, decimal number system uses 0-9 digits and octal number system uses 0-7 digits.

There are two ways in decimal to octal conversion,

  • using Integer.toOctalString() method.
  • with our own logic.

public static String toOctalString(int i)

Also read – decimal to hexadecimal conversion in java

toOctalString() method returns a the string representation of the unsigned integer value represented by the argument in octal (base 8).

import java.util.Scanner;
public class DecimalToOctal 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter decimal number: ");
      int number = sc.nextInt();
      String strOctal = Integer.toOctalString(number);
      System.out.println("Octal number is: " + strOctal);
      sc.close();
   }
}


Output:

Please enter decimal number: 143
Octal number is: 217


Before going with our own logic first let’s see decimal to octal conversion algorithm.

  1. User enters a number.
  2. Then find remainder of given number with 8.
  3. Now store the remainder.
  4. Divide number by 8.
  5. Repeat until number is not equal to zero using while loop.
  6. Finally reverse the number.
public class DecimalToOctalDemo 
{
   static void toOctal(int number) 
   { 
      // octalNumber array stores octal number 
      int[] octalNumber = new int[100]; 
      // here's the counter for octalNumber array 
      int a = 0; 
      while(number != 0)  
      { 
         // store remainder in octal array 
         octalNumber[a] = number % 8; 
         number = number / 8; 
         a++; 
      } 
      // print octal number array in reverse order 
      for(int b = a - 1; b >= 0; b--)
      {
         System.out.print(octalNumber[b]);
      }
   }
   public static void main(String[] args) 
   {
      int num = 65;
      toOctal(num);
   }
}


Output:

101


Click here to learn vice versa of above java program.

Also read – decimal to binary in java


using while loop

Let’s learn to convert decimal to octal using while loop.

import java.util.Scanner;
public class UsingWhileLoop
{
   public static void main(String[] args)
   {
      int decimal, a = 0;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter decimal number: ");
      decimal = sc.nextInt();
      System.out.println("Octal number is: ");
      int[] arr = new int[50];
      while(decimal != 0)
      {
         arr[a++] = decimal % 8;
         decimal = decimal / 8;
      }
      for(int d = a - 1; d >= 0; d--)
      {
         System.out.print(arr[d]);
      }
      sc.close();
   }
}


Output:

Please enter decimal number: 644
Octal number is: 1204