C# - Tower of Hanoi Program

In this C# program, we are implementing the solution of tower of Hanoi problem. By Nidhi Last updated : March 28, 2023

Tower of Hanoi

Here, we implement the Tower of Hanoi puzzle. In this puzzle, there are three rods and a number of discs with different sizes. In this puzzle, the puzzle starts with the discs in a stack in ascending order of size on one rod, the smallest at the top. Here we have to obtain the same stack on the 3rd rod.

C# program to solve the Tower of Hanoi puzzle

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.

C# Data Structure Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.