C# program to demonstrate the Tower Of Hanoi

Here, we are going to demonstrate the Tower Of Hanoi in C#.
Submitted by Nidhi, on November 05, 2020

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.

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.

//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 »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.