Home > OS >  Display a row of data in columns in C#
Display a row of data in columns in C#

Time:09-26

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#?

CodePudding user response:

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

  •  Tags:  
  • c#
  • Related