two dimension array in C#

 

For example: Write a program to demonstrate the use of two dimension array.  

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication6

{

class Program

    {

static void Main(string[] args)

        {

 

int i, j;

//Declaring multi dimensional array

int[,] a = new int[3, 2];

for (i = 0; i < 3; i++)

            {

for (j = 0; j < 2; j++)

                {

Console.Write("Enter the Matrix(3X2): ");

                    a[i, j] = Convert.ToInt32(Console.ReadLine());

                }

            }

Console.WriteLine("---------Output of Program------ ");

Console.WriteLine("All Entered Elements (3X2)");

 

//Formatting Output

//outer loop for accessing rows

for (i = 0; i < 3; i++)

            {

//inner or nested loop for accessing column of each row

for (j = 0; j < 2; j++)

                {

Console.Write("{0}\t", a[i,j]);

                }

Console.Write("\n");

            }

Console.ReadLine();

 

        }

    }

}

 

Output:

Comments

Post a Comment

Popular posts from this blog

simple program C#

Regular Expression Validator Control in asp.net