kernelthread.com

C#

The Towers of Hanoi as a C# program.

An introduction to C# can be found on Microsoft's Developer Network.

// // The Towers Of Hanoi // C# // Copyright (C) 2002 Amit Singh. All Rights Reserved. // http://hanoi.kernelthread.com // // Tested under Visual Studio .NET // using System; namespace Hanoi { class Solution { public static void doHanoi(int n, string f, string t, string u) { if (n <= 1) { System.Console.WriteLine(f + " --> " + t); } else { doHanoi(n - 1, f, u, t); System.Console.WriteLine(f + " --> " + t); doHanoi(n - 1, u, t, f); } } [STAThread] static void Main(string[] args) { if (args.Length != 1) { System.Console.WriteLine("usage: hanoi <number>"); System.Environment.Exit(1); } int n = System.Convert.ToInt32(args[0]); if ((n < 1) || (n > 10)) { System.Console.WriteLine( "usage: hanoi <number> /* 1 <= number <= 10 */"); System.Environment.Exit(1); } doHanoi(n, "1", "3", "2"); System.Environment.Exit(0); } } }

Download

hanoi.cs