Back to: C#.NET Programs and Algorithms

**Decimal to Octal Conversion in C# with Examples**

In this article, I am going to discuss **Decimal Number to Octal Number Conversion in C# **with Examples. Please read our previous article, where we discussed **Binary to Octal Number Conversion in C#****.** At the end of this article, you will understand how to convert a decimal number to an octal number in C#.

**Decimal Number System in Computer: **

Decimal is a term that describes the base-10 number system. The decimal number system consists of ten single-digit numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Example: (35)_{10}

**Octal Number System in Computer:**

The octal numeral system or oct describes the base-8 number system and uses the digits 0 to 7. Octal numerals can be made from binary numerals by grouping consecutive binary digits into groups of three (starting from the right). Example: (5303)_{8}

**Decimal to octal conversion example:**

Decimal number ((127)_{10}

- Divide 127 by 8. 127 ÷ 8= 15(Quotient) and (7) Remainder
- Divide 15 by 8 again. 15 ÷ 8 = 1(Quotient) and (7) Remainder
- Divide 1 by 8, we get. 1 ÷ 8 = 0(Quotient) and (1) Remainder

The quotient is 0 now, no more division can be possible. So, by taking the remainders in reverse order, we get the octal number. Hence, (127)_{10} = (177)_{8. }For better understanding, please have a look at the below image.

**Logic to Convert Decimal to Octal in C# **

To find the conversion of Decimal to Octal number simply need to follow the below steps:

- First, we divide the decimal numbers by 8.
- Then we get quotient and remainder.
- If the remainder is greater than 0 then again, we repeat step 1 and step 2.
- Then we store the number from bottom to top and we get the octal number

**Example: Decimal to Octal Conversion in C# **

The following C# Program will convert a decimal number to its equivalent octal number.

using System; class Demo { public static void Main() { int decValue, counter, i = 1, j; int[] octalValue = new int[80]; decValue = 127; counter = decValue; Console.WriteLine("Decimal Number:{0}", decValue); while (counter != 0) { octalValue[i++] = counter % 8; counter /= 8; } Console.Write("Octal Number: "); for (j = i - 1; j > 0; j--) Console.Write(octalValue[j]); Console.Read(); } }

**Output:**

**Example: Taking input from the user**

In the following C# program, we will take the decimal number from the user and then convert the decimal number to its equivalent octal number and print it on the console.

using System; public class DecimaltoOctal { public static void Main() { int i, j, Octal = 0, dn; Console.Write("Enter the Decimal Number : "); int DecimalNumber = Convert.ToInt32(Console.ReadLine()); dn = DecimalNumber; i = 1; for (j = DecimalNumber; j > 0; j = j / 8) { Octal = Octal + (j % 8) * i; i *= 10; DecimalNumber /= 8; } Console.WriteLine("The Octal Number is {1}.\n\n", dn, Octal); Console.ReadKey(); } }

**Output:**

**C# Code (Using function) **

In the below example, we have created one method to convert the decimal number to an octal number.

using System; class Decimaltooctal { static void DecToOctal(int n) { int[] octalNum = new int[100]; int i = 0; while (n != 0) { octalNum[i] = n % 8; n /= 8; i++; } Console.Write("Octal Number : "); for (int j = i - 1; j >= 0; j--) Console.Write(octalNum[j]); } public static void Main() { Console.Write("Enter the Decimal Number : "); int DecimalNumber = Convert.ToInt32(Console.ReadLine()); DecToOctal(DecimalNumber); Console.ReadKey(); } }

**Output:**

In the next article, I am going to discuss **Hexadecimal to Octal Conversion in C#** with Examples. Here, in this article, I try to explain **how to convert a decimal number to an octal number in C#** with Examples and I hope you enjoy this Decimal to Octal Conversion in C# article.

**About the Author: Pranaya Rout**

Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.