C# program to demonstrate the Tower Of Hanoi

We saw a lot of information in the previous article. We are going to know about this very clearly in the article C# program to demonstrate the Tower Of Hanoi. I think you will like this article very much. Let’s go into the article.

C# program to demonstrate the Tower Of Hanoi

C# program to demonstrate the Tower Of Hanoi

//C# program to demonstrate Tower Of Hanoi 

using System;

class TOH
{
    int discs;
    
    public TOH(int val)
    {
        discs = val;
    }

    public void MoveDiscs(int num, int from, int to, int other)
    {
        if (num > 0)
        {
            MoveDiscs( num- 1, from, other, to);
            Console.WriteLine("Move disk {0} from tower {1} to tower {2}",num, from, to);
            MoveDiscs(num - 1, other, to, from);
        }
    }
}
class Demo
{
    public static void Main()
    {
        TOH T;
        int total_discs;

        Console.Write("Enter the total number of discs: ");
        total_discs = int.Parse(Console.ReadLine());

        T = new TOH(total_discs);
        T.MoveDiscs(total_discs, 1, 3, 2);
    }
}
Enter the total number of discs: 5
Move disk 1 from tower 1 to tower 3
Move disk 2 from tower 1 to tower 2
Move disk 1 from tower 3 to tower 2
Move disk 3 from tower 1 to tower 3
Move disk 1 from tower 2 to tower 1
Move disk 2 from tower 2 to tower 3
Move disk 1 from tower 1 to tower 3
Move disk 4 from tower 1 to tower 2
Move disk 1 from tower 3 to tower 2
Move disk 2 from tower 3 to tower 1
Move disk 1 from tower 2 to tower 1
Move disk 3 from tower 3 to tower 2
Move disk 1 from tower 1 to tower 3
Move disk 2 from tower 1 to tower 2
Move disk 1 from tower 3 to tower 2
Move disk 5 from tower 1 to tower 3
Move disk 1 from tower 2 to tower 1
Move disk 2 from tower 2 to tower 3
Move disk 1 from tower 1 to tower 3
Move disk 3 from tower 2 to tower 1
Move disk 1 from tower 3 to tower 2
Move disk 2 from tower 3 to tower 1
Move disk 1 from tower 2 to tower 1
Move disk 4 from tower 2 to tower 3
Move disk 1 from tower 1 to tower 3
Move disk 2 from tower 1 to tower 2
Move disk 1 from tower 3 to tower 2
Move disk 3 from tower 1 to tower 3
Move disk 1 from tower 2 to tower 1
Move disk 2 from tower 2 to tower 3
Move disk 1 from tower 1 to tower 3
Press any key to continue . . .

Final Words

C# program to demonstrate the Tower Of Hanoi This article is your favorite topic because it is your favorite. And I will meet you in the next article.

Hi, I'm Selva a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment