[Math] How to print all possible combinations of five numbers selected from $\{1, 2, 3, \ldots, 10\}$

combinations

I need to print all possible combinations of five numbers selected from $\{1, 2, 3, \ldots, 10\}$. You can think of it like a lottery. You have $5$ numbers and each number can go from $1$ to $10$ and no number can be repeated.

How can I print all possible combinations?

My attempt was to add $1$ to the last number until it reaches the end ($10$), then do the same with the $4$th number until it reaches the end, and the same with $3$rd, $2$nd and $1$st.

1 2 3 4 5

1 2 3 4 6

………etc

…………10

1 2 3 5 10

1 2 3 6 10

……….etc

……….10 10

But as you can see, I repeat numbers and don't get all combinations.

Best Answer

For combinations:

    N=10;
    for(a=1; a <= N-4; ++a)
     for(b=a+1; b <= N-3; ++b)
      for(c=b+1; c <= N-2; ++c)
       for(d=c+1; d <= N-1; ++d)
        for(e=d+1; e <= N; ++e)
         print a,b,c,d,e

http://js.do/daveyp225/comb

For permutations:

    N=10;
    for(a=1; a <= N; ++a)
     for(b=1; b <= N; ++b)
      if(b different from a) 
       for(c=1; c <= N; ++c)
       if(c different from a,b) 
        for(d=1; d <= N; ++d)
        if(d different from a,b,c) 
         for(e=1; e <= N; ++e)
         if(e different from a,b,c,d) 
           print a,b,c,d,e

http://js.do/daveyp225/perm