Monday, July 21, 2008

Tower of Hanoi Code!!!

class hanoi
{
public static void main (String args[])
{

int numdisks = 3;
H_dohanoi(numdisks, 3, 1, 2);
System.exit(0);
}

static void H_dohanoi(int n, int t, int f, int u)
{
if (n > 0) {
H_dohanoi(n-1, u, f, t);
H_moveit(f, t);
H_dohanoi(n-1, t, u, f);
}
}

static void H_moveit(int from, int to)
{
System.out.print("move ");
System.out.print(from);
System.out.print(" --> ");
System.out.println(to);
}
}

Try running it with 6 pegs!

1 comment:

Unknown said...

how 2 do for n disks i mean more than 3 and dat 2 takin d input from d user..pls reply