What is the octal equivalent of a decimal number in C#?

To get the octal equivalent of a decimal number in C#, you need to repeatedly divide the decimal number by 8 and store the remainders. The octal number system uses base 8 (digits 0-7), while decimal uses base 10.

Syntax

Following is the basic algorithm for decimal to octal conversion −

while (decimal != 0) {
   remainder = decimal % 8;
   decimal = decimal / 8;
   // store remainder in array
}

How It Works

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

Decimal 12 to Octal Conversion 12 ÷ 8 Quotient: 1 Remainder: 4 1 ÷ 8 Quotient: 0 Remainder: 1 Result 14? Read remainders from bottom to top: 1, 4 Decimal 12 = Octal 14

Using Array to Store Octal Digits

Example

using System;

class Program {
   static void Main(string[] args) {
      int[] oct = new int[50];
      
      // decimal number
      int dec = 12;
      int original = dec;
      
      int i = 0;
      while (dec != 0) {
         oct[i] = dec % 8;
         dec = dec / 8;
         i++;
      }
      
      Console.Write("Decimal " + original + " 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 12 in octal is: 14

Using Built-in Convert Method

Example

using System;

class Program {
   static void Main(string[] args) {
      int decimal1 = 12;
      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 12 = Octal 14
Decimal 64 = Octal 100
Decimal 255 = Octal 377

Creating a Custom Conversion Method

Example

using System;

class Program {
   static string DecimalToOctal(int decimalNumber) {
      if (decimalNumber == 0) return "0";
      
      string octal = "";
      while (decimalNumber > 0) {
         octal = (decimalNumber % 8) + octal;
         decimalNumber /= 8;
      }
      return octal;
   }
   
   static void Main(string[] args) {
      int[] numbers = {12, 25, 100, 512};
      
      foreach (int num in numbers) {
         string octalResult = DecimalToOctal(num);
         Console.WriteLine("Decimal {0} = Octal {1}", num, octalResult);
      }
   }
}

The output of the above code is −

Decimal 12 = Octal 14
Decimal 25 = Octal 31
Decimal 100 = Octal 144
Decimal 512 = Octal 1000

Comparison of Methods

Method Advantages Use Case
Array Method Shows the algorithm clearly, educational Learning and understanding the conversion process
Convert.ToString() Built-in, simple, efficient Production code, quick conversions
Custom Method Reusable, returns string directly When you need custom formatting or validation

Conclusion

Converting decimal numbers to octal in C# can be done using manual division by 8 and collecting remainders, or using the built-in Convert.ToString() method with base 8. The manual approach helps understand the conversion algorithm, while the built-in method is more efficient for practical applications.

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

275 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements