Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โปรแกรม C# แปลงทศนิยมเป็นเลขฐานแปด


ตั้งเลขทศนิยม -

int decVal = 40;

ตอนนี้ใช้ตัวแปรและตั้งค่า decVal ในนั้น. หาเศษที่เหลือด้วย 8 เนื่องจากฐานแปดมีระบบเลขฐาน 8 และประเมินเป็นลูปเหมือนตัวอย่างโค้ดต่อไปนี้

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

ตัวอย่าง

คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อแปลงทศนิยมเป็นเลขฐานแปดได้

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

ผลลัพธ์

Decimal Number:40
Octal Number: 50