adsense


Showing posts with label JAVA EXAMPLES SOLVING TOWER OF HANOI. Show all posts
Showing posts with label JAVA EXAMPLES SOLVING TOWER OF HANOI. Show all posts

Sunday, 25 October 2015

JAVA EXAMPLES SOLVING TOWER OF HANOI PAGE 75

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES SOLVING TOWER OF HANOI



Java Examples - Solving Tower of Hanoi



Problem Description:
How to use method for solving Tower of Hanoi problem?
Solution:
This example displays the way of using method for solving Tower of Hanoi problem( for 3 disks).
public class MainClass {
   public static void main(String[] args) {
      int nDisks = 3;
      doTowers(nDisks, 'A', 'B', 'C');
   }
   public static void doTowers(int topN, char from,
   char inter, char to) {
      if (topN == 1){
         System.out.println("Disk 1 from "
         + from + " to " + to);
      }else {
         doTowers(topN - 1, from, to, inter);
         System.out.println("Disk "
         + topN + " from " + from + " to " + to);
         doTowers(topN - 1, inter, from, to);
      }
   }
}
Result:
The above code sample will produce the following result.
Disk 1 from A to C
Disk 2 from A to B
Disk 1 from C to B
Disk 3 from A to C
Disk 1 from B to A
Disk 2 from B to C
Disk 1 from A to C