import java.util.*;
class towers{
public static void main(String args[])
{
honoi('A','B','C',(long)1,n);
}
static int disk_no(long i,int n) // to find the next disk to move
{
if(i%2!=0)
return 1;
else
{
int j;
long k;
for(j=2;j<=n;j++)
for(k=0;k<Math.pow(2,n-j);k++)
if(i==(long)Math.pow(2,(long)j-1)*((2*k)+1))
{
System.out.println("returning :"+j);
return j;
}
return 0;
}
}
static void honoi(char x,char y,char z,long i,int n)
{
if(i<Math.pow(2,n))
{
switch((int)i%2)
{
case 0:System.out.println("STEP "+i+" :\tMove disk "+(long)disk_no(i,n)+" from pole "+z+" to "+y);
honoi(x,y,z,++i,n);break;
default:System.out.println("STEP "+i+" :\tMove disk "+(long)disk_no(i,n)+" from pole "+x+" to "+y);
honoi(y,z,x,++i,n);
}
}
}}
I read n value from user as number of disks ofcourse I skipped it here I thought that the problem is with disk_no() function if not mention the logical errors in the code if any