Java Decimal to Octal Conversion

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is Decimal to Octal Conversion?

Decimal (base 10) is the everyday number system. Octal (base 8) uses only digits 0–7. Conversion means rewriting a nonnegative integer using powers of eight instead of powers of ten.

Remember
Rightmost octal digit = 8⁰ (value 1)
Next digit             = 8¹ (value 8)
Next digit             = 8² (value 64)
…

Example: 57₁₀
  57 ÷ 8 → rem 1, then 7
  reverse → 71₈
  Check: 7×8 + 1 = 57
DecimalRemainders (LSD first)Octal
80, then 110
571, then 771
640, 0, then 1100

How to Convert Decimal to Octal in Java

Four clear steps — then pick a remainder loop or Integer.toOctalString.

  1. Accept a nonnegative integer n.
  2. If n == 0, print 0 immediately.
  3. Collect digits with n % 8 and n /= 8, then reverse — or call Integer.toOctalString(n).
  4. Print the octal digits.
MethodIdeaTimeBest for
Remainder + reverseStore n % 8, divide by 8, print reverseO(log n)Interviews, learning place values
Integer.toOctalStringLibrary formats n as base 8O(log n)Short answers, production printing

Pseudocode

Pseudocode
function DecimalToOctal(n):          // n >= 0
    if n == 0:
        print 0
        return

    digits = []
    while n > 0:
        digits.append(n mod 8)       // LSD first
        n = floor(n / 8)

    for i from length(digits) - 1 down to 0:
        print digits[i]

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Valid digitsOnly 0–7
Next digitn % 8
Shrink nn /= 8
OrderFirst remainder = rightmost digit
Built-inInteger.toOctalString(n)
Binary link1 octal digit = 3 binary bits
Classic check57 → 71

Live Preview

Enter a nonnegative integer to see remainders and the octal result.

Whole numbers n ≥ 0 (capped at 999999999). Tap a chip or type a value.

Result
Decimal    = 57
Remainders = 1, 7  (LSD → MSD)
Octal      = 71
Check      = 7×8 + 1 = 57

Worked Walkthrough — n = 57

Divide by 8. Stack remainders from right to left, then reverse for the final string.

Calculation
57 ÷ 8 = 7 remainder 1   ← LSD (rightmost)
 7 ÷ 8 = 0 remainder 7   ← MSD (leftmost)

Remainders collected: 1, 7
Reverse for output:   7, 1
Octal:                71

Verify: 7×8¹ + 1×8⁰ = 56 + 1 = 57 ✓

Java Programs

Start with the remainder loop so you can explain every digit. Then use Integer.toOctalString for a short library-style answer.

Example 1 — Remainder + Reverse

Store n % 8 digits LSD-first in an array, then print them from last index to first.

Java
class DecimalToOctal {
    static void decimalToOctal(int n) {
        int[] digits = new int[32];
        int count = 0;

        if (n < 0) {
            System.out.println("Use a nonnegative integer.");
            return;
        }
        if (n == 0) {
            System.out.println("0");
            return;
        }

        while (n > 0) {
            digits[count++] = n % 8;
            n /= 8;
        }

        for (int i = count - 1; i >= 0; i--) {
            System.out.print(digits[i]);
        }
        System.out.println();
    }

    public static void main(String[] args) {
        decimalToOctal(57); // 71
        decimalToOctal(0);  // 0
        decimalToOctal(64); // 100
    }
}

How It Works

  1. Reject negatives so the learning version stays focused on nonnegative conversion.
  2. Print 0 when n is zero — otherwise the loop would produce no output.
  3. While n > 0, store n % 8 (current LSD) and replace n with n / 8.
  4. Print from count - 1 down to 0 so the most significant digit comes first.
  5. For 57: remainders 1, 7 reverse to 71.
Trace — 57
n = 57 → store 1, n = 7
n = 7  → store 7, n = 0

digits = [1, 7]
print reverse → 71

Example 2 — Integer.toOctalString

Guard negatives, then let the standard library format the number as octal.

Java
class DecimalToOctalLib {
    static void decimalToOctal(int n) {
        if (n < 0) {
            System.out.println("Use a nonnegative integer.");
            return;
        }

        System.out.println(Integer.toOctalString(n));
    }

    public static void main(String[] args) {
        decimalToOctal(57); // 71
        decimalToOctal(0);  // 0
        decimalToOctal(8);  // 10

        // Bonus: same idea for binary groups
        System.out.println("71 in binary groups: 111 001");
        // 71₈ ↔ 111 001₂  (7=111, 1=001)
    }
}

How It Works

  1. Reject negatives so the contract matches the remainder method.
  2. Integer.toOctalString(n) returns the base-8 string of n.
  3. Zero is handled for you — it returns "0".
  4. Optional check: binary of 57 is 111001; grouped as 111 001 it recovers octal digits 7 and 1.
  5. Use the remainder loop in interviews when you must show the math; prefer toOctalString when you only need to print.

Edge Cases & Pitfalls

Check these before calling the solution done.

Zero

Special-case 0

A plain while (n > 0) loop prints nothing — print 0 first.

Order

Reverse remainders

Printing in collection order reverses the digits (57 would show as 17).

Negatives

Define a policy

This tutorial rejects negatives. Do not silently take an absolute value unless the problem says so.

Wrong base

Use % 8 and / 8

Copy-pasting a binary solution with % 2 produces binary, not octal.

Time and Space Complexity

ProgramTimeExtra space
Remainder + reverseO(log n)O(log n)
Integer.toOctalStringO(log n)O(log n) for the result string

Digit count is about log8 n for positive n.

Key Takeaways

  • Core loop: take n % 8, then n /= 8, until n is 0.
  • Reverse: first remainder is the rightmost digit.
  • Two options: manual remainder loop, or Integer.toOctalString.
  • Test set: 57 → 71; 64 → 100; 0 → 0.

One line: collect n % 8 remainders and reverse them — or call Integer.toOctalString.

Frequently Asked Questions

Repeatedly take n % 8 (the next digit) and n /= 8 until n becomes 0, then print the digits in reverse. Or use Integer.toOctalString(n).
The first remainder is the least significant digit. Printing from last remainder to first gives the usual left-to-right octal form.
A while (n > 0) loop never runs, so print 0 before the loop (or use Integer.toOctalString, which already returns "0").
It returns the octal string of an int. For example, Integer.toOctalString(57) returns "71".
One octal digit equals exactly three binary bits. Octal 7 is binary 111; octal 71 is binary 111001.
The remainder method produces O(log₈ n) digits, which is O(log n). Extra space for the digit buffer is also O(log n).

Next: Disarium Number

Check whether a number equals the sum of its digits raised to their positions.

Disarium number tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

18 people found this page helpful