C# program to convert decimal number to octal number

WBOY
Release: 2023-09-17 17:49:02
forward
601 people have browsed it

C# 将十进制数转换为八进制数的程序

Set Decimal -

int decVal = 40;
Copy after login

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;
}
Copy after login

Example

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

Output

Decimal Number:40
Octal Number: 50
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!