From 61f2f48861912ae2905e63cc2f071e380191af12 Mon Sep 17 00:00:00 2001 From: Zameer Ali Date: Thu, 13 Oct 2022 20:45:24 +0500 Subject: [PATCH] recursion Tower of hanio --- Data-Structures/Java/hanoi.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Data-Structures/Java/hanoi.java diff --git a/Data-Structures/Java/hanoi.java b/Data-Structures/Java/hanoi.java new file mode 100644 index 0000000..8b86e79 --- /dev/null +++ b/Data-Structures/Java/hanoi.java @@ -0,0 +1,16 @@ +public class hanoi { + public void Tower_of_Hanoi(int n, char source, char destination, char helper) { + if (n == 1) { + System.out.println("Move disk 1 from " + source + " to " + destination); + return; + } + Tower_of_Hanoi(n - 1, source, helper, destination); + System.out.println("Move disk " + n + " from " + source + " to " + destination); + Tower_of_Hanoi(n - 1, helper, destination, source); + } + + public static void main(String[] args) { + hanoi obj = new hanoi(); + obj.Tower_of_Hanoi(3, 'A', 'B', 'C'); + } +}