C# program to convert decimal to Octal number

CsharpServer Side ProgrammingProgramming

Set the decimal number −

int decVal = 40;

Now take a variable and set the decVal in it. Find the remainder with 8 since octal has base-8 number system and evaluate it in a loop like the following code snippet.

while (quot != 0) {
   octalVal[i++] = quot % 8;
   quot = quot / 8;
}

Example

You can try to run the following code to convert decimal to octal number.

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();
   }
}

Output

Decimal Number:40
Octal Number: 50
raja
Published on 25-Jul-2018 09:45:47
Advertisements