import java.io.*; class Hanoi { static void Hanoi(int ndisks, int frompeg, int topeg) { int thirdpeg; if (ndisks == 1) { /* ----- This is easy and I can do it ! ----- */ System.out.println("Move a disk from peg " + frompeg + " to " + topeg); } else { /* ------ This is tough, pass the buck... ------ */ thirdpeg = 6 - frompeg - topeg; // Find the free peg... Hanoi(ndisks-1, frompeg, thirdpeg); // solve this for me... System.out.println("Move a disk from peg " + frompeg + " to " + topeg); Hanoi(ndisks-1, thirdpeg, topeg); // solve this for me... } } public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); int n; System.out.print("How many disks: "); n = Integer.parseInt(stdin.readLine()); Hanoi(n, /* from */ 1, /* to */ 3); } }