Set Decimal -
int decVal = 40;
Now, take a variable and set decVal to it. Since octal has an 8-based number system, take the remainder by 8 and calculate it in a loop as shown in the code snippet below.
while (quot != 0) { octalVal[i++] = quot % 8; quot = quot / 8; }
You can try running the following code to convert decimal to octal.
Live Demo
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.Read(); } }
Decimal Number:40 Octal Number: 50
The above is the detailed content of C# program to convert decimal number to octal number. For more information, please follow other related articles on the PHP Chinese website!