Basic Programming C# Examples

C# Program to Create an Identity Matrix

The Program in C# Program to Create an Identity Matrix is given below:

using System;

namespace IdentityMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the size of the identity matrix :");
            int size = int.Parse(Console.ReadLine());
            int[,] matrix = CreateIdentityMatrix(size);
            PrintMatrix(matrix);
        }

        static int[,] CreateIdentityMatrix(int size)
        {
            int[,] matrix = new int[size, size];
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (i == j)
                    {
                        matrix[i, j] = 1;
                    }
                    else
                    {
                        matrix[i, j] = 0;
                    }
                }
            }
            return matrix;
        }

        static void PrintMatrix(int[,] matrix)
        {
            int size = matrix.GetLength(0);
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    Console.Write(matrix[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

Output:


Hello Codeauri Family, enter the size of the identity matrix :

1 0 0
0 1 0
0 0 1

Pro-Tips💡

Here are the step by step execution of above program:

  1. The using System directive at the top of the file includes the System namespace in the program, which provides access to various system-level functions, including the Console class used in this code.
  2. The code defines a namespace IdentityMatrix that encapsulates all of the code in the program.
  3. The Program class contains the program’s main method, Main, which is the entry point of the program.
  4. In the Main method, the program prompts the user to enter the size of the identity matrix by writing a message to the console. The size is then read from the console and stored in the size variable.
  5. The CreateIdentityMatrix method is then called with the size parameter. This method returns an identity matrix of the specified size, which is stored in the matrix variable.
  6. The PrintMatrix method is then called with the matrix parameter, which prints the contents of the matrix to the console, row by row.
  7. The CreateIdentityMatrix method initializes a two-dimensional integer array matrix of the specified size and sets the diagonal elements to 1 and the rest to 0. This is done by using nested for loops to iterate over the rows and columns of the array. If the row and column indices are equal (i == j), the current element is set to 1, otherwise it is set to 0.
  8. The PrintMatrix method prints the contents of the matrix to the console, row by row. This is done by using nested for loops to iterate over the rows and columns of the matrix and printing the elements, separated by a space. After each row is printed, a line break is added to separate the rows.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X