How to convert a Decimal to Octal using C#?

To convert a decimal number to its octal equivalent in C#, we use the division method where we repeatedly divide the decimal number by 8 and collect the remainders. The octal number system uses base 8, meaning it only uses digits 0-7.

How It Works

The conversion process involves dividing the decimal number by 8 repeatedly and storing the remainders in reverse order. Each remainder represents a digit in the octal representation.

Decimal to Octal Conversion Process 18 ÷ 8 = 2 R 2 2 ÷ 8 = 0 R 2 Read remainders from bottom to top: 22? 18?? = 22? Decimal 18 equals Octal 22

Syntax

The basic algorithm uses a while loop to repeatedly divide by 8 −

while (decimal != 0) {
    octal[i] = decimal % 8;  // store remainder
    decimal = decimal / 8;   // divide by 8
    i++;
}

Using Array to Store Octal Digits

Example

using System;

class Program {
    static void Main(string[] args) {
        int[] oct = new int[30];
        
        // decimal number to convert
        int dec = 18;
        int originalDec = dec;
        
        int i = 0;
        while (dec != 0) {
            oct[i] = dec % 8;
            dec = dec / 8;
            i++;
        }
        
        Console.Write("Decimal " + originalDec + " in octal is: ");
        for (int j = i - 1; j >= 0; j--) {
            Console.Write(oct[j]);
        }
        Console.WriteLine();
    }
}

The output of the above code is −

Decimal 18 in octal is: 22

Using Built-in Convert Method

C# provides a built-in method Convert.ToString() that can convert decimal numbers to different bases including octal (base 8) −

Example

using System;

class Program {
    static void Main(string[] args) {
        int decimal1 = 18;
        int decimal2 = 64;
        int decimal3 = 255;
        
        string octal1 = Convert.ToString(decimal1, 8);
        string octal2 = Convert.ToString(decimal2, 8);
        string octal3 = Convert.ToString(decimal3, 8);
        
        Console.WriteLine($"Decimal {decimal1} = Octal {octal1}");
        Console.WriteLine($"Decimal {decimal2} = Octal {octal2}");
        Console.WriteLine($"Decimal {decimal3} = Octal {octal3}");
    }
}

The output of the above code is −

Decimal 18 = Octal 22
Decimal 64 = Octal 100
Decimal 255 = Octal 377

Using Recursive Method

Example

using System;

class Program {
    static string ConvertToOctal(int decimalNumber) {
        if (decimalNumber == 0) {
            return "0";
        }
        
        if (decimalNumber 

The output of the above code is −

Decimal 18 = Octal 22
Decimal 25 = Octal 31
Decimal 100 = Octal 144
Decimal 512 = Octal 1000

Comparison of Methods

Method Advantages Best For
Array-based Loop Shows the algorithm clearly, educational Learning the conversion process
Convert.ToString() Simple, built-in, handles edge cases Production code, quick conversions
Recursive Method Clean, functional approach When recursion fits your coding style

Conclusion

Converting decimal to octal in C# can be achieved through manual division by 8 with remainder collection, using the built-in Convert.ToString() method, or implementing a recursive solution. The Convert.ToString() method is the most practical for real applications.

Updated on: 2026-03-17T07:04:35+05:30

460 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements