Java convert decimal to octal

Let’s learn java convert decimal to octal.

java convert decimal to octal

Java program for decimal to octal conversion

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).

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

There are two ways,

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

public static String toOctalString(int i)

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.out.print("Please enter decimal number: ");
      int number = sc.nextInt();
      String strOctal = Integer.toOctalString(number);
      System.out.println("To octal: " + strOctal);


Please enter decimal number: 143
To octal: 217

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

  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.
  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; 
      // print octal number array in reverse order 
      for(int b = a - 1; b >= 0; b--)

   public static void main(String[] args) 
      int num = 65;



Click here to learn vice versa of above java program.