Skip to content

Instantly share code, notes, and snippets.

@rekkusu
Created April 25, 2010 07:44
Show Gist options
  • Save rekkusu/378246 to your computer and use it in GitHub Desktop.
Save rekkusu/378246 to your computer and use it in GitHub Desktop.
using System;
namespace Dec2Bin
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Dec2Bin - 10進数から2進数に変換");
Console.WriteLine("プログラム能力を試すのにお薦め");
while (true)
{
Console.Write("> ");
string input = Console.ReadLine();
try
{
int dec = int.Parse(input);
if (dec < 0) throw new ApplicationException();
Console.WriteLine("Result: " + input + " -> " + dec2bin(dec));
}
catch (FormatException)
{
break;
}
catch (ApplicationException)
{
Console.WriteLine("負の数値は変換に対応していません");
}
}
}
static string dec2bin(int input)
{
if (input == 0) return "0";
return dec2bin(input / 2) + input % 2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment