// the tower of Hanoi problem class RecursiveEx1 { static int disks = 3; public static void main (String[] args) { Hanoi (disks, 'A', 'B', 'C'); System.out. println ("Total number of moves: " + countMoves (disks)); } public static void Hanoi (int numberOfDisks, char from, char temp, char to) { if (numberOfDisks == 1) System.out.println ("Disk 1 moved from " + from + " to " + to); else { Hanoi (numberOfDisks-1, from, to, temp); System.out.println ("Disk " + numberOfDisks + " moved from " + from + " to " + to); Hanoi (numberOfDisks-1, temp, from, to); } } // end Hanoi public static int countMoves (int disks) { if (disks == 0) return 0; else return (1 + (2 * countMoves (disks - 1))); } }