Pages

Monday, November 4, 2013

Show user input number value in a 10 by 10 grid in console

Here i show you some simple c# code sample which explain about how to show user input number value in a 10 * 10 grid as a console output.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace gridview
{
    class Program
    {
        static void Main(string[] args)
        {
            // ask user input
            Console.WriteLine("Enter a coordinate between 00 to 99");
            Console.WriteLine("");

            Console.Write("coordinate value is : ");

            // get user input value from console and assign to varable
            int coordinate = int.Parse(Console.ReadLine());

            // find the row
            int row = coordinate / 10;

            // find the column
            int column = coordinate % 10; 

            Console.WriteLine("");
            Console.Write(" ");

            // write column number
            for (int i = 0; i < 10; i++)
            {
                Console.Write(i);
            }

            Console.WriteLine("");

            // find the row number we use for loop
            for (int index = 0; index < 10; index++)
            {
                if (index == row)
                {
                    Console.Write(index);

                    for (int jIndex = 0; jIndex < 10; jIndex++)
                    {
                        if (jIndex == column)
                        {
                            // show user input with "X" symbol in console
                            Console.WriteLine("x");
                            break;
                        }
                        Console.Write(" ");
                    }
                }

                else
                {
                    Console.WriteLine(index);
                }
            }
         Console.ReadLine();              
        }
    }
}

The console output shows like below


1 comment:

  1. Very informative and well written post! Quite interesting and nice topic chosen for the post.
    Fujitsu Laptops

    ReplyDelete