Skip to content

Instantly share code, notes, and snippets.

@LaurentiuGabriel
Created August 7, 2022 15:15
Show Gist options
  • Select an option

  • Save LaurentiuGabriel/ef67797a8450f43fbcd1915581846cb7 to your computer and use it in GitHub Desktop.

Select an option

Save LaurentiuGabriel/ef67797a8450f43fbcd1915581846cb7 to your computer and use it in GitHub Desktop.
Tower of Hanoi YouTube Challenge
public class Tower_of_Hanoi {
static void towerOfHanoi(int n, char first_rod, char third_rod, char second_rod){
if (n == 1) {
System.out.println("Move disk 1 from rod " + first_rod + " to rod " + third_rod);
return;
}
towerOfHanoi(n - 1, first_rod, second_rod, third_rod);
System.out.println("Move disk " + n + " from rod " + first_rod + " to rod " + third_rod);
towerOfHanoi(n - 1, second_rod, third_rod, first_rod);
}
public static void main(String[] args) {
int n = 3;
towerOfHanoi(n, 'A', 'C', 'B');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment