Klicker Klicker - 1 year ago 36
C# Question

Increment groups of numbers in c# .net

In the scenario where you have two groups of numbers, each containing the numbers

1
to
10
, how would you go about incrementing each number until it hits its upper limit?

For example, to procude the numbers as follows:

1.1
1.2
1.3


...


1.9
1.10
2.1
2.2
2.3


...


2.9
2.10


etc...

My effort is pretty abysmal so far - I started with for loops and couldnt get a decent result, moved onto a while loop and still havent got a working solution, but here's what I have so far.

class Program
{
static void Main()
{
int group01 = 1;
int group01Max = 10;

int group02 = 2;
int group02Max = 10;

while (group01 < group01Max)
{
while (group02 < group02Max)
{
Console.WriteLine(string.Format("{0}.{1}", group01, group02));
}
}
}
}

Answer Source

I'm not entirely sure if this is what you're asking, but do you mean something like this?

a = 10
b = 10
for(int i = 0; i < a; i++){
    string tmp = i.toString();
    for(int j = 0; j < b; j++){
        tmp += "." + j.toString();
    }
    Console.WriteLine(tmp);
}

edit: using $"" notation would simplify this as done in the answer below

a = 10
b = 10
for(int i = 0; i < a; i++){;
    for(int j = 0; j < b; j++){
        Console.WriteLine($"{i}.{j}");
    }

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download