JaguarV12
JaguarV12

Reputation: 13

Display a row of data in columns in C#

I have a list of data that I have calculated (all in C#)

I want to display the result of the calculated data in 4 columns instead of one long row.

This is what I have in my prompt right now:

1
2
3
4 etc

This is what I want:

1     2     3     4  
5     6     7     8  etc

How can I do that in C#?

Upvotes: 1

Views: 355

Answers (1)

Voodoo
Voodoo

Reputation: 1658

private static void Print(int[] arr)
{
    int counter = 0;
    
    foreach (int item in arr)
    {
        if (counter == 4) // can be configured to desired columns
        {
            counter = 0;
            Console.WriteLine();
        }
        counter++;
        Console.Write(item + " ");
    }
}

INPUT: {1,2,3,4,5,6,7,8,9,10,11,12,13}

OUTPUT:

1 2 3 4 

5 6 7 8

9 10 11 12

13

Demo Fiddler: Here

Upvotes: 2

Related Questions