Advertisement
ตัวอย่างการเขียนโปรแกรมแปลงเลขฐานสิบเป็นเลขฐานสอง โดยใช้ภาษา C# เขียนในรูปแบบของ Console Application
Advertisement
/*
* DekDEV.com
*/
using System;
namespace Dec2Bin
{
class Program
{
static void Main(string[] args)
{
Console.Write("\nEnter Decimal number : ");
int dec = int.Parse(Console.ReadLine());
Console.WriteLine("\nBinary number is " + Dec2Bin(dec));
Console.WriteLine("\n\nPowered by dekdev.com");
Console.Read();
}
static string Dec2Bin(int value)
{
int remain = value;
string val = "";
while (remain > 0){
if (remain >= 2)
{
val = (remain % 2) + val;
}
else
{
val = remain + val;
}
remain = remain / 2;
}
return val;
}
}
}

Advertisement