0

I challenged you to write the code of tower of hanoi 😊

this program should perform through recursion

27th Oct 2016, 7:23 PM
mirza arslan
mirza arslan - avatar
2 Réponses
+ 3
first tell me what is the tower of Hanoi
22nd Nov 2016, 9:25 PM
AKIL
AKIL - avatar
+ 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"); } }
8th Nov 2016, 8:04 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar