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);
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s