diff --git a/TowerOfHanoi Algorithm/Tower of Hanoi b/TowerOfHanoi Algorithm/Tower of Hanoi new file mode 100644 index 00000000..fc658413 --- /dev/null +++ b/TowerOfHanoi Algorithm/Tower of Hanoi @@ -0,0 +1,21 @@ +package com.JournalDev; +public class Main { + static void towerOfHanoi(int n, char from_rod, char to_rod, char helper_rod) + { + if (n == 1) + { + System.out.println("Take disk 1 from rod " + from_rod + " to rod " + to_rod); + return; + } + towerOfHanoi(n-1, from_rod, helper_rod, to_rod); + System.out.println("Take disk " + n + " from rod " + from_rod + " to rod " + to_rod); + towerOfHanoi(n-1, helper_rod, to_rod, from_rod); + } + + public static void main(String args[]) + { + int n = 5; + towerOfHanoi(n,'A','C', 'B'); + } + +}