Skip to content
This repository has been archived by the owner on Aug 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #938 from hyunjiLeeTech/issue-697-HyunjiLee
Browse files Browse the repository at this point in the history
Create Tower_Of_Hanoi in C#
  • Loading branch information
ahampriyanshu committed Oct 7, 2020
2 parents f582725 + 7741d02 commit fde9056
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Algorithms/Dynamic_Programming/Tower_Of_Hanoi/Tower_Of_Hanoi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;

namespace Tower_Of_Hanoi
{
class Tower_Of_Hanoi
{
static void Main(string[] args)
{
int numOfDisks;
char source = 'A', auxiliary = 'B', destination = 'C';
Console.WriteLine("The Tower of Hanoi consists of three rods and a number of disks of different sizes.");
Console.Write("Enter the number of disks: ");

numOfDisks = Convert.ToInt32(Console.ReadLine());

if(numOfDisks > 0)
{
TowerOfHanoi(numOfDisks, source, auxiliary, destination);
}
else
{
Console.WriteLine("The number should be greater than 0");
}
}

public static void TowerOfHanoi(int numDisk, char source, char auxiliary, char destination)
{

if (numDisk > 1)
{
TowerOfHanoi(numDisk - 1, source, auxiliary, destination);
Console.WriteLine("Move disk " + numDisk + " from source " + source + " to destination " + destination);
TowerOfHanoi(numDisk - 1, auxiliary, destination, source);
}
else
{
Console.WriteLine("Move disk 1 from cource " + source + " to destination " + destination);
}

}
}
}

0 comments on commit fde9056

Please sign in to comment.