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
Decimal
Remainders (LSD first)
Octal
8
0, then 1
10
57
1, then 7
71
64
0, 0, then 1
100
Approach
How to Convert Decimal to Octal in Python
Four clear steps — then pick a remainder loop or the built-in formatter.
Accept a nonnegative integer n.
If n == 0, return "0" immediately.
Collect digits with n % 8 and n // 8, then reverse — or call oct(n)[2:].
Print or return the octal string.
Method
Idea
Time
Best for
Remainder + reverse
Append n % 8, divide by 8, reverse digits
O(log n)
Interviews, learning place values
oct(n)[2:]
Format n as base 8 in one call
O(log n)
Production code, short answers
Pseudocode
Pseudocode
function DecimalToOctal(n): // n >= 0
if n == 0:
return "0"
digits = []
while n > 0:
digits.append(n mod 8) // LSD first
n = floor(n / 8)
reverse(digits)
return join(digits)
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Valid digits
Only 0–7
Next digit
n % 8
Shrink n
n = n // 8
Order
First remainder = rightmost digit
Built-in
oct(n)[2:] or format(n, "o")
Binary link
1 octal digit = 3 binary bits
Classic check
57 → 71
Try it
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.