Towers of Hanoi

import java.io.*;

class TowerOfHanoi
{
public static void main()throws IOException
{
hanoi obj=new hanoi();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter number of rings”);
int N=Integer.parseInt(br.readLine());
obj.han(‘A’,’B’,’C’,N);
}

}
class hanoi
{
void han(char a,char b,char c,int n)
{
if(n==0)
System.out.println(“WRONG INPUT !!!”);
if(n==1)
System.out.println(“Move from “+a+” to “+c);
if(n>1)
{
han(a,c,b,n-1);
han(a,b,c,1);
han(b,a,c,n-1);
}
}
}

Leave a comment