- Spotlight
82 = 64
Convert Decimal to Octal in C#
What you’ll learn
- How division by eight and remainders give octal digits
0–7. - 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.uintfor 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.
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.
5757 ÷ 8 is 7 remainder 1. Then 7 ÷ 8 is 0 remainder 7. Remainders from rightmost digit outward: 1, then 7 → octal 71.
Quick snapshots
- 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.
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
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 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.
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.
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.
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
🔄 Input / output examples
Example 1 uses 57 and 0; Example 2 prints the same values with the recursive helper.
| Decimal | Octal |
|---|---|
0 | 0 |
7 | 7 |
8 | 10 |
57 | 71 |
64 | 100 |
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).
n == 0
while (n != 0) never runs; print 0 explicitly.
Remainder range
For nonnegative n, n % 8 is always 0–7.
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.
Very large values
For enormous integers use iteration or a stack so you do not rely on deep call chains.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Remainder + array | About one step per octal digit | Digits stored in the array |
| Recursion (Example 2) | Same digit count | One stack frame per digit |
Convert.ToString(n, 8) | Implementation-defined; linear in output length | Allocates a string |
For nonnegative n, the number of octal digits grows slowly as n grows.
Summary
- Idea: peel
n % 8, dividenby8, reverse for display—or recurse MSB-first. - Code: guard
0;uintkeeps nonnegative math straightforward. - Bonus: octal digits align with binary in triplets.
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.
8 people found this page helpful
