This repository has been archived by the owner on Aug 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #938 from hyunjiLeeTech/issue-697-HyunjiLee
Create Tower_Of_Hanoi in C#
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
Algorithms/Dynamic_Programming/Tower_Of_Hanoi/Tower_Of_Hanoi.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} | ||
} |