Convert Decimal to Octal in Java



In this article, we will learn to convert decimals to octals in Java. The octal number system (base 8) is often used in computing and digital systems. It uses digits from 0 to 7, and converting a decimal number (base 10) to an octal is a common task in programming.

Problem Statement

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

Below is a demonstration of the same ?

Input

int decimalNumber = 8;

Output

The octal value is
10

Different Approaches

Following are the two approaches to converting decimal numbers to octal in Java ?

Using Manual Conversion 

One of the most straightforward ways to convert a decimal number to an octal is by repeatedly dividing the decimal number by 8 and storing the remainder. The remainders, when read in reverse order, form the octal equivalent of the decimal number.

Algorithm

Following are the steps to convert decimal numbers to octal using manual conversion ?

  • Step 1: START
  • Step 2: Declare three integer values namely my_input, ,I and j and an integer array my_octal
  • Step 3: Read the required values from the user/ define the values
  • Step 4: Using a while condition of input not equal to 0, compute my_input % 8 and store it to my_octal[i]
  • Step 5: Compute my_input / 8 and assign it to ?my_input', increment the ?i' value.
  • Step 6: Iterating using a for loop, print the ?my_octal' array
  • Step 7: Stop
i = 0;
      while (my_input != 0) {
         my_octal[i] = my_input % 8;
         my_input = my_input / 8;
         i++;
      }

Example

Below is an example of converting decimal numbers to octal using manual conversion ?

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]);
   }
}

Output

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 built-in method in Java, Integer.toOctalString(), provides a straightforward and efficient way to convert a decimal number to an octal. It eliminates the need for manual calculations, making the code simpler and easier to implement. 

String octal = Integer.toOctalString(decimalNumber);

Example

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

public class DecimalToOctalUsingBuiltin {
   public static void main(String[] args) {
      int decimalNumber = 8; // Define the decimal number
      String octal = Integer.toOctalString(decimalNumber);
      System.out.println("The decimal number is: " + decimalNumber);
      System.out.println("The octal value is: " + octal);
   }
}

Output

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

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

Comparison Table

Feature Manual Conversion Using Integer.toOctalString()
Ease of Use More complex due to manual calculations Very simple and concise
Performance Efficient but involves loops and array usage Highly optimized built-in method
Educational Value High, as it explains the underlying logic Limited, but quicker for implementation
Updated on: 2025-01-28T14:57:41+05:30

657 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements