Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to convert decimal to Octal number
A decimal number can be converted to an octal number using the division method. Octal is a base-8 number system that uses digits 0-7. To convert from decimal to octal, we repeatedly divide the decimal number by 8 and collect the remainders.
How It Works
The conversion process involves dividing the decimal number by 8 repeatedly and storing the remainders. The octal equivalent is formed by reading the remainders in reverse order −
Using Manual Division Method
Example
using System;
class Demo {
public static void Main() {
int decVal, quot, i = 1, j;
int[] octalVal = new int[80];
decVal = 40;
quot = decVal;
Console.WriteLine("Decimal Number: {0}", decVal);
while (quot != 0) {
octalVal[i++] = quot % 8;
quot = quot / 8;
}
Console.Write("Octal Number: ");
for (j = i - 1; j > 0; j--) {
Console.Write(octalVal[j]);
}
Console.WriteLine();
}
}
The output of the above code is −
Decimal Number: 40 Octal Number: 50
Using Convert.ToString() Method
C# provides a built-in method to convert decimal to octal using Convert.ToString() with base 8 −
Example
using System;
class Program {
public static void Main() {
int decimalNumber = 40;
string octalNumber = Convert.ToString(decimalNumber, 8);
Console.WriteLine("Decimal Number: " + decimalNumber);
Console.WriteLine("Octal Number: " + octalNumber);
// Converting multiple numbers
int[] numbers = {15, 64, 255};
Console.WriteLine("\nMultiple conversions:");
foreach(int num in numbers) {
Console.WriteLine("{0} (decimal) = {1} (octal)",
num, Convert.ToString(num, 8));
}
}
}
The output of the above code is −
Decimal Number: 40 Octal Number: 50 Multiple conversions: 15 (decimal) = 17 (octal) 64 (decimal) = 100 (octal) 255 (decimal) = 377 (octal)
Using Custom Method with String Builder
Example
using System;
using System.Text;
class OctalConverter {
public static string DecimalToOctal(int decimalNum) {
if (decimalNum == 0) return "0";
StringBuilder octal = new StringBuilder();
while (decimalNum > 0) {
octal.Insert(0, decimalNum % 8);
decimalNum /= 8;
}
return octal.ToString();
}
public static void Main() {
int[] testNumbers = {0, 8, 40, 100, 512};
Console.WriteLine("Decimal to Octal Conversion:");
Console.WriteLine("----------------------------");
foreach(int num in testNumbers) {
string octalResult = DecimalToOctal(num);
Console.WriteLine("{0,3} decimal = {1} octal", num, octalResult);
}
}
}
The output of the above code is −
Decimal to Octal Conversion: ---------------------------- 0 decimal = 0 octal 8 decimal = 10 octal 40 decimal = 50 octal 100 decimal = 144 octal 512 decimal = 1000 octal
Comparison of Methods
| Method | Advantages | Use Case |
|---|---|---|
| Manual Division | Shows the mathematical process clearly | Educational purposes, understanding the algorithm |
| Convert.ToString() | Built-in, simple, efficient | Production code, quick conversions |
| StringBuilder Method | Memory efficient for large numbers | Custom implementations, performance-critical code |
Conclusion
Converting decimal to octal in C# can be done using manual division by 8, the built-in Convert.ToString() method, or custom implementations. The Convert.ToString(decimalNumber, 8) method is the most practical approach for most applications, while manual methods help understand the underlying mathematical process.
