Java program – Gray code

by Nideesh C on April 29, 2011 · 0 comments

in Java Programming




public class Beckett {

    public static void moves(int n, boolean forward) {
        if (n == 0) return;
        moves(n-1, true);
        if (forward) System.out.println("enter " + n);
        else         System.out.println("exit  " + n);
        moves(n-1, false);
    }

    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        moves(N, true);
    }

}


/*************************************************************************
 *  Execution:    java Beckett N
 *
 *  Prints instructions for a Beckett play with N actors.
 *
 *  % java Beckett 1
 *  enter 1
 *
 *  % java Beckett 2
 *  enter 1
 *  enter 2
 *  exit  1
 *
 *  % java Beckett 3
 *  enter 1
 *  enter 2
 *  exit  1
 *  enter 3
 *  enter 1
 *  exit  2
 *  exit  1
 *
 *************************************************************************/



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Java program – Towers of Hanoi
  2. Java program – Integer multiplication and division
  3. Java program – Euclid’s algorithm
  4. Java program – While loop
  5. Java program – Harmonic numbers

Leave a Comment

Previous post:

Next post: