Convert Decimal to Octal in Java



The octal number system has a base value of 8 as it contains digits from 0 to 7, i.e., 8. On the other hand, the decimal number system has 10 digits from 0 to 9. Hence, its base value is 10. In this article, we will learn to convert decimals to the octal number system in Java.

Problem Statement

The goal is to write a program to convert a given decimal number (base 10) into its equivalent octal number (base 8).

Example Scenario:

Input: int decimalNumber = 8
Output: The octal value is = 10

Converting Decimal to Octal in Java

Following are the ways to convert decimal numbers to octal in Java:

Brute Force Approach

The brute force approach to convert the decimal number to an octal is as follows:

  • Divide the decimal number by 8.
  • Next, divide the quotient and repeat this step until we get the quotient less than 8.
  • After the division, read the remainder from bottom to top to obtain the octal number from the given decimal number.

Example

Below is an example of converting decimal numbers to octal using the above-discussed way:

public class DecimalToOctal {
   public static void main(String[] args){
      int my_input, i, j;
      my_input = 8;
      System.out.println("The decimal number is defined as " +my_input);
      int[] my_octal = new int[100];
      System.out.println("The octal value is ");
      i = 0;
      while (my_input != 0) {
         my_octal[i] = my_input % 8;
         my_input = my_input / 8;
         i++;
      }
      for ( j = i - 1; j >= 0; j--)
         System.out.print(my_octal[j]);
   }
}

On running the above code, it will print the octal number of the specified decimal number:

The decimal number is defined as 8
The octal value is
10

Time Complexity: O(log_8(n).

Space Complexity: O(log_8(n), due to the array for storing remainders).

Using Integer.toOctalString()

The Integer.toOctalString() is a built-in method in Java. It takes a decimal value of integer type as an argument and returns a String representation of that integer value after converting it to an octal number. Internally, it uses base 8 for the conversion operation.

Example

Below is an example of converting decimal numbers to octal using Integer.toOctalString() -

public class DecOctal {
   public static void main( String args[] ) {
      // initialization of decimal value
      int decimalVal = 99;
      System.out.println("The given decimal number is: " + decimalVal);
      // converting decimal to octal
      String octalVal = Integer.toOctalString(decimalVal);
      // printing the result
      System.out.println("The decimal number after converting to octal is: " +  octalVal);
   }
}

Output of the above code is given below:

The given decimal number is: 99
The decimal number after converting to octal is: 143

Time Complexity: O(log_8(n).
Space Complexity: O(1), no additional storage is needed).

Using Integer.toString()

The Integer.toString() is also a built-in method in Java. We will pass a decimal number and the base value of octal as arguments to this method so that the decimal number gets converted to octal.

Example

Let's see a Java program of converting decimal numbers to octal using Integer.toString() method-

public class DecOctal {
   public static void main( String args[] ) {
      // initialization of decimal value
      int decimalVal = 99;
      System.out.println("The given decimal number is: " + decimalVal);
      // converting decimal to octal
      String octalVal = Integer.toString(decimalVal, 8);
      // printing the result
      System.out.println("The decimal number after converting to octal is: " +  octalVal);
   }
}

Output of the above code is given below:

The given decimal number is: 99
The decimal number after converting to octal is: 143
Updated on: 2025-06-06T13:04:10+05:30

682 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements