tower of hanoi program in java using recursion
// Java recursive program to solve tower of hanoi puzzle
class GFG
{
// Java recursive function to solve tower of hanoi puzzle
static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
System.out.println("Move disk 1 from rod " + from_rod + " to rod " + to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
// Driver method
public static void main(String args[])
{
int n = 4; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
}
}
Also in Java:
- how to declare array java
- how to create a thread local variable in java
- fibonacci sequence in java recursion
- connecting to h2 database from java
- java initialize string array
- Don't use a line-beased input after a token-based input.
- how to ask user for his location in android
- java create folder
- java append to array
- check if string is null or empty java
- write file from a specific location in java
- java script dynamic varibale creation
- boolean parse jtextfield
- java 8 seconds to days
- java merge sort
- java code to save excel data to mysql
- while loops java
- deleting elements of an array in java
- store string elements in character array in java
- multiple string java
- spigot get player from UUID
- Matlab java.lang.IllegalArgumentException: Invalid range
- fragment button nullpointerexception
- java nextpermutation