## Thursday, May 26, 2016

### Tower of Hanoi program in java

Hi Guys , Hope you all are doing well , as we all know that the most populate and most favorite question of interview is Tower of hanoi problem and most of the guy fells that the tower of hanoi program is very much complex.

in my earlier post I have talked about
Bubble Sort
Selection Sort
Insertion Sort

So lets see how complex the tower of hanoi program is

Java code of Tower of Hanoi Program

public class TowerOfHanoi {
public static void main(String[] args) {
move(3 ,'A','C','B');
}
public static void move(int noOfDisc ,char from,char to,char inter)
{
if(noOfDisc==1) System.out.println("Moving Disk "+noOfDisc+" from "+from+" to "+to);
else{
move(noOfDisc-1 ,from, inter,to);
System.out.println("Moving Disk "+noOfDisc+" from "+from+" to "+to);
move (noOfDisc-1,inter,to,from);
// System.out.println("Moving Disk "+noOfDisc+" from "+from+" to "+to);
}
}
Output:-
Moving Disk 1 from A to C
Moving Disk 2 from A to B
Moving Disk 1 from C to B
Moving Disk 3 from A to C
Moving Disk 1 from B to A
Moving Disk 2 from B to C
Moving Disk 1 from A to C

In this program we use the recursion algorithm to calculate the steps required to move the plates from one to another tower.
If you have any problem , do comment below