Convert Decimal to Octal in C#

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Base conversion

What you’ll learn

  • How division by eight and remainders give octal digits 07.
  • Why zero needs its own branch and why digits are printed backwards compared to how we collect them.
  • A recursive MSB-first print without an array, a live preview, and how octal pairs with binary in groups of three bits.

Overview

Octal is base eight: every position is a power of eight, and each digit is only 0 through 7. To turn a normal decimal whole number into octal, keep dividing by 8 and save the remainders; then print those remainders from last to first—same recipe as decimal-to-binary, but the divisor is 8 instead of 2.

Two programs

Array + reverse print (like the classic homework), then recursion so digits appear MSB-first without reversing manually.

Live preview

Nonnegative safe integers via toString(8) in the browser.

Same pattern as binary

Swap divisor 2 for 8; the story stays identical.

Prerequisites

Integer division and modulo, arrays, loops, and optional recursion.

  • using System;, class Program, Console.Write / WriteLine.
  • uint for nonnegative values keeps division behaviour predictable.

Decimal to octal

Think of octal as counting in buckets of eight. The remainder after dividing by eight tells you how many “ones” are left in the current place; then you divide by eight to move one column to the left.

Classic homework example: decimal 57. Because 57 = 7 × 8 + 1, the octal digits (reading high place to low place) are 7 then 1, written 71 in base eight.

Decimal base 10
Octal base 8
Digits 0 – 7

Division recipe

For nonnegative n: record n % 8, replace n by n / 8 using integer division, repeat until n is zero. The remainders, read from last obtained to first, form the octal numeral.

57

57 ÷ 8 is 7 remainder 1. Then 7 ÷ 8 is 0 remainder 7. Remainders from rightmost digit outward: 1, then 7 → octal 71.

Quick snapshots

64 100 (oct)
Spotlight
82 = 64
57 71 (oct)
Breakdown
7·8 + 1

Hint: every octal digit sits cleanly on top of three binary digits—that is why engineers still reach for octal when bits come in threes.

Live preview

Nonnegative integers within JavaScript’s safe range. Uses toString(8) so you can compare with your loops.

Negative numbers are not handled here; decide separately how you want to treat signs.

Live result
Press “Show octal” to convert.

Algorithm (remainder method)

Goal: print octal digits for nonnegative n, highest place value first.

If n == 0

Print 0 and stop. A bare while (n != 0) loop skips this case.

Collect digits

While n > 0: save n % 8, then n /= 8.

Print reversed

Output saved digits from last saved down to first.

📜 Pseudocode

Pseudocode
function printOctalFromDecimal(n):  // n ≥ 0
    if n = 0:
        output "0"; return
    digits ← empty list
    while n > 0:
        append (n mod 8) to digits
        n ← floor(n / 8)
    print digits in reverse order
1

Divide by eight with reverse print

Same idea as the original walkthrough: store remainders, print backward. Uses uint, a sensible buffer size, and an explicit zero branch plus a second call so learners see the edge case.

c#
using System;

class Program
{
    static void DecimalToOctal(uint decimalNumber)
    {
        int[] octalDigits = new int[16];
        int i = 0;
        uint n = decimalNumber;

        if (n == 0)
        {
            Console.WriteLine("Octal equivalent: 0");
            return;
        }

        while (n != 0)
        {
            octalDigits[i] = (int)(n % 8);
            n /= 8;
            i++;
        }

        Console.Write("Octal equivalent: ");
        for (int j = i - 1; j >= 0; j--)
        {
            Console.Write(octalDigits[j]);
        }

        Console.WriteLine();
    }

    static void Main()
    {
        DecimalToOctal(57);
        DecimalToOctal(0);
    }
}

Explanation

Every remainder is automatically between 0 and 7, so each slot prints as a single decimal digit—no letters until bases above ten.

for (int j = i - 1; j >= 0; j--)

MSB first. The last remainder you pushed belongs to the largest power of eight.

2

Recursive MSB-first (no digit array)

Print higher octal places before lower ones: recurse on n / 8 when n ≥ 8, then write n % 8. The base case n == 0 prints a single 0.

c#
using System;

class Program
{
    static void PrintOctalMsbRecursive(uint n)
    {
        if (n >= 8)
        {
            PrintOctalMsbRecursive(n / 8);
        }

        Console.Write(n % 8);
    }

    static void Main()
    {
        Console.Write("57 in octal: ");
        PrintOctalMsbRecursive(57);
        Console.WriteLine();

        Console.Write("0 in octal: ");
        PrintOctalMsbRecursive(0);
        Console.WriteLine();
    }
}

Explanation

Depth equals the number of octal digits—tiny for uint. For huge arbitrary-precision numbers you would usually prefer iteration.

Console.Write(n % 8);

No A–F symbols. Octal stays inside ordinary digits.

Optimization and library path

Convert.ToString(n, 8). Quick string for nonnegative integers once you understand the manual algorithm.

From binary. Group bits in threes from the right to jump between binary and octal without repeated division.

Interview: state nonnegative input, handle n == 0, mention the three-bit grouping trick.

❓ FAQ

Octal is base eight. The remainder n % 8 is the rightmost octal digit (how many ones you have in the eights place before carrying). Integer division n / 8 removes that digit and shifts the rest, just like dividing by 10 in decimal or by 2 in binary.
The first remainder you get is the ones digit in octal. Later remainders belong to higher places. Printing from last remainder to first matches how we read numbers left to right.
A loop like while (n != 0) never runs, so you must print 0 yourself. Otherwise you might print only the label and a blank line.
For nonnegative integers, Convert.ToString(n, 8) builds the octal string. Use it after you can explain the division-by-eight loop by hand.
One octal digit always matches exactly three binary digits. Group binary bits in threes from the right to jump between bases quickly.
Roughly one loop step per octal digit, so O(log n) in base 8 for the magnitude of n—that is a small number of steps for normal int-sized values.

🔄 Input / output examples

Example 1 uses 57 and 0; Example 2 prints the same values with the recursive helper.

DecimalOctal
00
77
810
5771
64100

Edge cases and pitfalls

These samples assume nonnegative values. Negative integers need an agreed rule (often you work with absolute value or show raw two's complement elsewhere).

Zero

n == 0

while (n != 0) never runs; print 0 explicitly.

Digits

Remainder range

For nonnegative n, n % 8 is always 07.

C# literals

Leading zeros

Unlike C, C# does not treat a leading 0 on an integer literal as octal. Use explicit conversion or bases when you mean octal.

Recursion

Very large values

For enormous integers use iteration or a stack so you do not rely on deep call chains.

⏱️ Time and space complexity

ApproachTimeExtra space
Remainder + arrayAbout one step per octal digitDigits stored in the array
Recursion (Example 2)Same digit countOne stack frame per digit
Convert.ToString(n, 8)Implementation-defined; linear in output lengthAllocates a string

For nonnegative n, the number of octal digits grows slowly as n grows.

Summary

  • Idea: peel n % 8, divide n by 8, reverse for display—or recurse MSB-first.
  • Code: guard 0; uint keeps nonnegative math straightforward.
  • Bonus: octal digits align with binary in triplets.
Did you know?

Each octal digit lines up with a block of three binary bits. That is why Unix permissions and some older manuals still count in base eight—it lines up neatly with bits.

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.

8 people found this page helpful