jonbon
jonbon

Reputation: 1200

Generate Combinations for a set of Numbers

In C#, I want to generate combinations for {1,2,3,4,5,6,7,8,9,0} in 5 digits. So, I want to get an output of 11111,11112, etc up to 99999.

When I searched I didn't get anything that could work when I threw it into a console application.

Everything always got an error with Combinations...

Upvotes: 3

Views: 2312

Answers (3)

DiverseAndRemote.com
DiverseAndRemote.com

Reputation: 19888

do a for loop and count from 11111 to 99999:

for(int i = 11111; i<=99999; i++){
    var combination = i.ToString();
    Console.WriteLine(combination);
}

or if you want 00001 to 99999

for (int i = 0; i <= 99999; i++)
{
    var combination = String.Format("{0:D5}", i);
    Console.WriteLine(combination);
}

Upvotes: 7

Thinking Sites
Thinking Sites

Reputation: 3542

If you're looking for a way to combine numbers, not specifically to get a sequence, you can do a linq query for it.

         var bob = new [] {1,2,3,4,5,6,7,8,9,0};
         var greg =
             from a in bob
             from b in bob
             from c in bob
             from d in bob
             from e in bob
             select string.Concat(a, b, c, d, e);

Upvotes: 2

perh
perh

Reputation: 1698

Simply counting from 0 to 99999 will produce all combinations (and you really should start with 00000 if you want all combinations)

Upvotes: 2

Related Questions