In C#, what is the octal equivalent of a decimal number?

王林
Release: 2023-08-22 19:45:12
forward
989 people have browsed it

In C#, what is the octal equivalent of a decimal number?

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

First, for the decimal value, use a while loop and store the remainder in an array set for octal. Here we find their modulo 8 in the array.

After that, divide the number by 8 −

while (dec != 0) {

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

Let’s look at 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();
      }
   }
}
Copy after login

The above is the detailed content of In C#, what is the octal equivalent of a decimal number?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template