Home  >  Article  >  Backend Development  >  C# program to convert decimal number to octal number

C# program to convert decimal number to octal number

WBOY
WBOYforward
2023-09-17 17:49:02618browse

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

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

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

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete