This was a typical introduction to programming question. Create the pattern of a chess board that is 8 x 8.  Use X and O to represent the squares. This is what your output should look like.

XOXOXOXO
OXOXOXOX
XOXOXOXO
OXOXOXOX
XOXOXOXO
OXOXOXOX
XOXOXOXO
OXOXOXOX

Below is a simple code l wrote to get this output. This is just for fun.

using System;

namespace ChessDashboard
{
class Program
{
static void Main(string[] args)
{
int i,j,remainder;
for(i=1;i <= 8;i++)
{
if (i % 2 == 0) { remainder = 1; }
else {remainder = 0;}
for (j = 1; j <=8; j++)
{
if (j % 2 == remainder )
{ Console.Write(“O”); }
else { Console.Write(“X”); }
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

What l’m doing is pretty straight forward, l have two for loops. The outside for loop corresponds to getting the 8 rows expected and the inside for loop corresponds to getting the 8 columns each alternating with X or 0. I used the mode operator to determine if the current i is divisible by 2 or not which helps me decide how to start outputting the current row with an X or O. When l complete outputing 8 columns, l move to the next line with writeline(). The readline() at the end is just to allow the output visible untill the user presses any key.

 

Leave a Reply