0
I challenged you to write the code of tower of hanoi 😊
this program should perform through recursion
2 Réponses
+ 3
first tell me what is the tower of Hanoi
+ 2
Too easy:
import java.util.Scanner;
public class Main{
public void solve(int n, String start, String auxiliary, String end) {
if (n == 1) {
System.out.println(start + " -> " + end);
} else {
solve(n - 1, start, end, auxiliary);
System.out.println(start + " -> " + end);
solve(n - 1, auxiliary, start, end);
}
}
public static void main(String[] args) {
Main towersOfHanoi = new Main();
System.out.print("Enter number of discs: ");
Scanner scanner = new Scanner(System.in);
int discs = scanner.nextInt();
towersOfHanoi.solve(discs, "A", "B", "C");
}
}