Use a pair of nested loops to create an X pattern - CSharp Language Basics

CSharp examples for Language Basics:for

Description

Use a pair of nested loops to create an X pattern

Demo Code

using System;/*from  w  w w  . j av  a2s . co m*/
public class Program
{
   public static void Main(string[] args)
   {
      int consoleWidth = 40;
      for(int rowNum = 0;rowNum < consoleWidth;rowNum += 2)
      {
         for (int columnNum = 0; columnNum < consoleWidth; columnNum++)
         {
            char c = ' ';
            if (columnNum == rowNum)
            {
               c = '\\';
            }
            int mirrorColumn = consoleWidth - rowNum;
            if (columnNum == mirrorColumn)
            {
               c = '/';
            }
            Console.Write(c);
         }
         Console.WriteLine();
      }
   }
}

Result


Related Tutorials