title: algorithm for generating number combinations without repetition
body: <p>I checked almost every similar post in here but I couldn't figure out how I can do what I want. What I am trying is to give an input in a C program, let say number 4, and the program return the following numbers in an array:</p>
  
  <pre><code>1
  2
  3
  4
  12
  13
  14
  23
  24
  34
  123
  134
  124
  1234
  </code></pre>
  
  <p>To be more clear:
  If the input number is 4 then i want to use digits 1-4 and generate all the possible combinations of digits(from 1-digit combinations to 4-digit combinations) without digit repetitions.</p>
  
  <p>I tried the following code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  /* Prints out a combination like {1, 2} */
  void printc(int comb[], int k) {
      printf("{");
      int i;
      for (i = 0; i &lt; k; ++i)
          printf("%d, ", comb[i] + 1);
      printf("\\b\\b}\\n");
  }
  
  
      int next_comb(int comb[], int k, int n) {
      int i = k - 1;
      ++comb[i];
      while ((i &gt;= 0) &amp;&amp; (comb[i] &gt;= n - k + 1 + i)) {
          --i;
          ++comb[i];
      }
  
      if (comb[0] &gt; n - k) /* Combination (n-k, n-k+1, ..., n) reached */
          return 0; /* No more combinations can be generated */
  
      /* comb now looks like (..., x, n, n, n, ..., n).
      Turn it into (..., x, x + 1, x + 2, ...) */
      for (i = i + 1; i &lt; k; ++i)
          comb[i] = comb[i - 1] + 1;
  
      return 1;
  }
  
  int main(int argc, char *argv[]) {
      int n = 5; /* The size of the set; for {1, 2, 3, 4} it's 4 */
      int k = 3; /* The size of the subsets; for {1, 2}, {1, 3}, ... it's 2 */
      int comb[16]; /* comb[i] is the index of the i-th element in the
              combination */
  
      /* Setup comb for the initial combination */
      int i;
      for (i = 0; i &lt; k; ++i)
          comb[i] = i;
  
      /* Print the first combination */
      printc(comb, k);
  
      /* Generate and print all the other combinations */
      while (next_comb(comb, k, n))
          printc(comb, k);
  
      return 0;
  }
  </code></pre>
  
  <p>The above program prints the result. I want to get the result somehow.. but I can't because the above code prints the result in a strange manner.</p>