w3resource

C#: Read a 2D array of size 3x3 and print the matrix

C# Sharp Array: Exercise-18 with Solution

Write a program in C# Sharp for a 2D array of size 3x3 and print the matrix.

Sample Solution:-

C# Sharp Code:

using System;

public class Exercise14  
{  
    public static void Main() 
    {
        int i, j; // Declare variables for row and column indices
        int[,] arr1 = new int[3, 3]; // Declare a 2D array of size 3x3 to store integers

        // Display a message prompting the user to input elements for a 3x3 matrix
        Console.Write("\n\nRead a 2D array of size 3x3 and print the matrix :\n");
        Console.Write("------------------------------------------------------\n");  

        // Input elements into the matrix
        Console.Write("Input elements in the matrix :\n");
        for (i = 0; i < 3; i++)
        {
            for (j = 0; j < 3; j++)
            {
                Console.Write("element - [{0},{1}] : ", i, j);
                arr1[i, j] = Convert.ToInt32(Console.ReadLine()); // Store user input in the 2D array
            }
        }  

        // Display the matrix
        Console.Write("\nThe matrix is : \n");
        for (i = 0; i < 3; i++)
        {
            Console.Write("\n");
            for (j = 0; j < 3; j++)
            {
                Console.Write("{0}\t", arr1[i, j]); // Print elements of the 2D array
            }
        }
        Console.Write("\n\n");
    }
}

Sample Output:

Read a 2D array of size 3x3 and print the matrix :                                                            
------------------------------------------------------                                                        
Input elements in the matrix :                                                                                
element - [0,0] : 1                                                                                           
element - [0,1] : 2                                                                                           
element - [0,2] : 3                                                                                           
element - [1,0] : 4                                                                                           
element - [1,1] : 5                                                                                           
element - [1,2] : 6                                                                                           
element - [2,0] : 7                                                                                           
element - [2,1] : 8                                                                                           
element - [2,2] : 9                                                                                           
                                                                                                              
The matrix is :                                                                                               
                                                                                                              
1       2       3                                                                                             
4       5       6                                                                                             
7       8       9

Flowchart :

Flowchart: Read a 2D array of size 3x3 and print the matrix

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to find the second smallest element in an array.
Next: Write a program in C# Sharp for addition of two Matrices of same size.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.