Tower of Hanoi Program in C#

This is a C# Program to demonstrate tower of hanoi.

Problem Description

This C# Program uses recursive function & solves the tower of hanoi.

Problem Solution

The tower of hanoi is a mathematical puzzle. It consists of three rods and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top. We have to obtain the same stack on the third rod.

Program/Source Code

Here is source code of the C# Program to Demonstrate Tower Of Hanoi. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Demonstrate Tower Of Hanoi
 */
using System;
class TowerOfHanoi
{
    int m_numdiscs;
    public TowerOfHanoi()
    {
        numdiscs = 0;
    }
    public TowerOfHanoi(int newval)
    {
        numdiscs = newval;
    }
    public int numdiscs
    {
        get
        {
            return m_numdiscs;
        }
        set
        {
            if (value > 0)
                m_numdiscs = value;
        }
     }
     public void movetower(int n, int from, int to, int other)
     {
         if (n > 0)
         {
             movetower(n - 1, from, other, to);
             Console.WriteLine("Move disk {0} from tower {1} to tower {2}", 
                                n, from, to);
             movetower(n - 1, other, to, from);
         }
     }
}
class TowersOfHanoiApp
{
    public static int Main()
    {
        TowerOfHanoi T = new TowerOfHanoi();
        string cnumdiscs;
        Console.Write("Enter the number of discs: ");
        cnumdiscs = Console.ReadLine();
        T.numdiscs = Convert.ToInt32(cnumdiscs);
        T.movetower(T.numdiscs, 1, 3, 2);
        Console.ReadLine();
        return 0;
    }
}
Program Explanation

In this C# Program, we are reading the number of discs using ‘cnumdiscs’ variable. Perform the movetower() function by passing ‘numdiscs’, 1, 3, 2 variable values as an argument.

advertisement
advertisement

The tower of hanoi is a mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top. We have to obtain the same stack on the third rod.

We are reading the number of discs using ‘cnumdiscs’ variable. Perform the movetower() function by passing the numdiscs, 1, 3, 2 variable values as an argument. Using if condition statement check the condition the value of ‘n’ variable is greater than 0. If the condition is true then execute the statement by moving the discs recursively.

Runtime Test Cases
 
Enter the Number of Disks : 2
Move Disk 1 from Tower 1 to Tower 2
Move Disk 1 from Tower 1 to Tower 3
Move Disk 1 from Tower 2 to Tower 3

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.