Example of c implementing the Tower of Hannott problem

  • 2020-06-19 11:37:14
  • OfStack

Tower of Hanoi: The Tower of Hanoi problem stems from an ancient legend of an Indian educational toy. Here is an example of c# implementing the Tower of Hannot


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace  Hanoi 
{
    class Program
    {
        static void hanoi(char A, char B, char C, int count)
        {
            if (count == 1)
                Console.WriteLine("1: " + A + "->" + B);
            else
            {
                hanoi(A, C, B, count - 1);
                Console.WriteLine(count + ": " + A + "->" + B);
                hanoi(C, B, A, count - 1);
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine(" Please enter the number of disks :");
            int N = 0;
            N = Convert.ToInt32(Console.ReadLine());
            hanoi('A', 'B', 'C', N);
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}


Related articles: