Rifai Rifai - 3 months ago 16
C# Question

Nested for loop c#

I have a school project and I need to complete it but its not working. I have to do a nested for loop in c# , I have managed to do the earlier projects which had the result :

1 2 3 6

1 2 3 6

1 2 3 6

1 2 3 6

static void Main(string[] args)
{
for (int row = 0; row < 4; row++)
{
int x = 1;
int y = 0;
for (int column = 0; column < 3; column++)
{
Console.Write(x);
y = y + x;
x = x + 1;
}
Console.Write(y);
Console.WriteLine();
}
Console.ReadLine();
}


but now i stuck with the second project which must give an output :

1 2 3 6

2 3 4 9

3 4 5 12

5 6 7 18

please help anyone !!

Answer
for (var x = 1; x < 5; x++)
{
   if (x == 4) x = 5;
   Console.Write("{0} ",x);
   for (var y = 1; y < 4; y++)
       Console.Write("{0} ", y < 3 ? x + y : 3 * (x + 1));
   Console.WriteLine();      
}