0
0
JavaHow-ToBeginner · 2 min read

Java Program to Convert Decimal to Octal Number

In Java, convert a decimal number to octal by using Integer.toOctalString(decimalNumber) or by repeatedly dividing the number by 8 and collecting remainders.
📋

Examples

Input8
Output10
Input65
Output101
Input0
Output0
🧠

How to Think About It

To convert decimal to octal, think of dividing the decimal number by 8 repeatedly. Each division gives a remainder between 0 and 7, which forms the octal digits from right to left. Collect these remainders until the number becomes zero.
📐

Algorithm

1
Get the decimal number as input.
2
While the number is greater than zero, divide it by 8 and record the remainder.
3
Store each remainder as an octal digit starting from the right.
4
Reverse the collected digits to get the octal number.
5
If the input is zero, the octal is also zero.
6
Return or print the octal number.
💻

Code

java
public class DecimalToOctal {
    public static void main(String[] args) {
        int decimalNumber = 65;
        String octalNumber = Integer.toOctalString(decimalNumber);
        System.out.println("Octal of " + decimalNumber + " is: " + octalNumber);
    }
}
Output
Octal of 65 is: 101
🔍

Dry Run

Let's trace converting decimal 65 to octal using repeated division.

1

Divide 65 by 8

65 ÷ 8 = 8 remainder 1 (rightmost octal digit)

2

Divide 8 by 8

8 ÷ 8 = 1 remainder 0 (next octal digit)

3

Divide 1 by 8

1 ÷ 8 = 0 remainder 1 (leftmost octal digit)

Decimal NumberDivide by 8QuotientRemainder (Octal Digit)
6565 ÷ 881
88 ÷ 810
11 ÷ 801
💡

Why This Works

Step 1: Using Integer.toOctalString

Java's built-in method Integer.toOctalString() converts decimal to octal by handling the division and remainder internally.

Step 2: Manual division method

Dividing the decimal number by 8 repeatedly and collecting remainders builds the octal number from right to left.

Step 3: Reversing remainders

Since the first remainder is the least significant digit, reversing the collected digits gives the correct octal representation.

🔄

Alternative Approaches

Manual conversion using loop and StringBuilder
java
public class DecimalToOctalManual {
    public static void main(String[] args) {
        int decimal = 65;
        StringBuilder octal = new StringBuilder();
        int number = decimal;
        if (number == 0) {
            octal.append('0');
        }
        while (number > 0) {
            int remainder = number % 8;
            octal.insert(0, remainder);
            number /= 8;
        }
        System.out.println("Octal of " + decimal + " is: " + octal);
    }
}
This method shows the step-by-step process but is longer than using built-in methods.
Using Integer.toString with radix 8
java
public class DecimalToOctalRadix {
    public static void main(String[] args) {
        int decimal = 65;
        String octal = Integer.toString(decimal, 8);
        System.out.println("Octal of " + decimal + " is: " + octal);
    }
}
This is a concise alternative using <code>Integer.toString()</code> with base 8.

Complexity: O(log n) time, O(log n) space

Time Complexity

The conversion requires dividing the number by 8 repeatedly until it reaches zero, which takes about log base 8 of n steps.

Space Complexity

Storing the octal digits requires space proportional to the number of digits, which is also about log base 8 of n.

Which Approach is Fastest?

Using built-in methods like Integer.toOctalString() or Integer.toString(number, 8) is fastest and less error-prone compared to manual division.

ApproachTimeSpaceBest For
Integer.toOctalString()O(log n)O(log n)Quick and simple conversion
Manual division with loopO(log n)O(log n)Learning and understanding conversion process
Integer.toString(number, 8)O(log n)O(log n)Concise alternative to toOctalString
💡
Use Integer.toOctalString() for quick and reliable decimal to octal conversion in Java.
⚠️
Beginners often forget to reverse the collected remainders when converting manually, resulting in incorrect octal digits order.