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


To get the octal equivalent of a decimal in C# −

Firstly, for the decimal value use a while loop and store the remainder in the array set for octal. Here we found the mod 8 of them in the array.

After that, divide the number by 8 −

while (dec != 0) {

   oct[i] = dec % 8;
   dec = dec / 8;
   i++;
}

Let us see the complete code. Here, our decimal number is 12 −

Example

using System;

namespace Demo {
   class Program {
      static void Main(string[] args) {

         int []oct = new int[50];

         // decimal
         int dec = 12;

         int i = 0;
         while (dec != 0) {
            oct[i] = dec % 8;
            dec = dec / 8;
            i++;
         }

         for (int j = i - 1; j >= 0; j--)
         Console.Write(oct[j]);

         Console.ReadKey();
      }
   }
}

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 21-Jun-2020

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements