From 2c85ca5179b083ff139b20f6164e4df9f3d21f2d Mon Sep 17 00:00:00 2001 From: Pushkar Singh <81770626+Pushkar03@users.noreply.github.com> Date: Sat, 8 Oct 2022 00:32:56 +0530 Subject: [PATCH] Tower of Hanoi in Java --- Java/Algorithms/Tower of Hanoi | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Java/Algorithms/Tower of Hanoi diff --git a/Java/Algorithms/Tower of Hanoi b/Java/Algorithms/Tower of Hanoi new file mode 100644 index 0000000..1f16553 --- /dev/null +++ b/Java/Algorithms/Tower of Hanoi @@ -0,0 +1,14 @@ +public class TowersOfHanoi { + public static void move(int n, int startPole, int endPole) { + if (n == 0) { + return; + } + int intermediatePole = 6 - startPole - endPole; + move(n-1, startPole, intermediatePole); + System.out.println("Move " +n + " from " + startPole + " to " +endPole); + move(n-1, intermediatePole, endPole); + } + public static void main(String[] args) { + move(5, 1, 3); + } +}