The source code to demonstrate the Tower Of Hanoi is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//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);
}
}
Output:
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 . . .
Explanation:
In the above program, we created two classes TOH and Demo. The TOH class contains data member discs, parameterized constructor, and MoveDiscs() method.
The constructor is used to initialize the total number of discs. The MoveDisc() method is a recursive method to implement the tower of Hanoi puzzle to move discs in 3 rods recursively.
Now look to the Demo class that contains the Main() method. The Main() method is the entry point for the program. Here, we created the object of TOH class and input the total number of discs and move the discs to implement the puzzle.
Program:
The source code to demonstrate the Tower Of Hanoi is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
In the above program, we created two classes TOH and Demo. The TOH class contains data member discs, parameterized constructor, and MoveDiscs() method.
The constructor is used to initialize the total number of discs. The MoveDisc() method is a recursive method to implement the tower of Hanoi puzzle to move discs in 3 rods recursively.
Now look to the Demo class that contains the Main() method. The Main() method is the entry point for the program. Here, we created the object of TOH class and input the total number of discs and move the discs to implement the puzzle.