title: Java - Recursion - Counting all combinations
body: <p>i had a question regarding counting all possible combinations. For example if i have a recursive method that gives me an output of:
  a. 0 0 
  b. 0 1
  c. 1 0
  d. 1 1</p>
  
  <p>in this case i have 8 possible outputs. how should i count them using java?
  i tried to use a counter but its giving me 4.</p>
  
  <p>Please help.
  Thanks.
  here is my code.</p>
  
  <pre><code>public static void printAllAssignments(char set[], int k) {
      int n = set.length;
      printAllAssignments(set, "", n, k);
  }
  
  // The main recursive method to print all possible strings of length k
   public static void printAllAssignments(char set[], String prefix, int n, int k) {
  
      // Base case: k is 0, print prefix
      if (k == 0) {
          counterTotalAssignments++;
          System.out.println("Occupancies: " + prefix);
          return;
      }
  
      // One by one add all characters from set and recursively
      // call for k equals to k-1
      for (int i = 0; i &lt; n; ++i) {
  
          // Next character of input added
          String newPrefix = prefix + set[i];
  
          // k is decreased, because we have added a new character
          printAllAssignments(set, newPrefix, n, k - 1);
  
      }
  }
  
  public static void main(String[] args) {
   char charSet[] = {'0', '1'};
      int k = 2;
      printAllAssignments(charSet, k);
      System.out.println("Total number of assignments: " + counterTotalAssignments);
  }
  
  output:
  Occupancies: 00
  Occupancies: 01
  Occupancies: 10
  Occupancies: 11
  Total number of assignments: 4
  </code></pre>