Q:

C# program to check the matrix is an identity matrix or not

belongs to collection: C# Basic Programs | array programs

0

C# program to check the matrix is an identity matrix or not

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to check the matrix is an identity matrix or not, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to check the matrix is an identity matrix or not.

using System;

class MatrixDemo
{
    public static void Main(string[] args)
    {
        int i = 0;
        int j = 0;

        int order = 0;
        int isIdentity = 0;

        int[,] Matrix= new int[5, 5];

        Console.Write("Enter the order for matrix: ");
        order = int.Parse(Console.ReadLine());

        Console.Write("Enter the elements of matrix: ");
        for (i = 0; i < order; i++)
        {
            for (j = 0; j < order; j++)
            {
                Matrix[i, j] = int.Parse(Console.ReadLine());
            }
        }
        
        Console.WriteLine("\nMatrix: ");
        for (i = 0; i < order; i++)
        {
            for (j = 0; j < order; j++)
            {
                Console.Write(Matrix[i, j] + "\t");

            }
            Console.WriteLine();
        }

        for (i = 0; i < order; i++)
        {
            for (j = 0; j < order; j++)
            {
                if ((i == j && Matrix[i, j] != 1) || (i != j && Matrix[i, j] != 0))
                {
                    isIdentity = 1;
                    break;
                }
            }
        }

        if(isIdentity==1)
            Console.WriteLine("Matrix is not an identity matrix"); 
        else
            Console.WriteLine("Matrix is an identity matrix");
    }
}

Output:

Enter the order for matrix: 2
Enter the elements of matrix: 1
0
0
1

Matrix:
1       0
0       1
Matrix is an identity matrix
Press any key to continue . . .

Explanation:

In the above program, we created a class MatrixDemo that contains a Main() method. The Main() method is the entry point for the program, Here, we created a 2-D array to represent a matrix. Then read the order for matrix.

Console.Write("Enter the elements of matrix: ");
for (i = 0; i < order; i++)
{
    for (j = 0; j < order; j++)
    {
        Matrix[i, j] = int.Parse(Console.ReadLine());
    }
}
Console.WriteLine("\nMatrix: ");
for (i = 0; i < order; i++)
{
    for (j = 0; j < order; j++)
    {
        Console.Write(Matrix[i, j] + "\t");

    }
    Console.WriteLine();
}

Here, we read the elements of the matrix from the user and then printed the matrix on the console screen.

for (i = 0; i < order; i++)
{
    for (j = 0; j < order; j++)
    {
        if ((i == j && Matrix[i, j] != 1) || (i != j && Matrix[i, j] != 0))
        {
            isIdentity = 1;
            break;
        }
    }
}

if(isIdentity==1)
    Console.WriteLine("Matrix is not an identity matrix"); 
else
    Console.WriteLine("Matrix is an identity matrix");

In the above code, we wrote code to check the input matrix is an identity matrix or not. If the value of the "isIdentity" variable is assigned to 1, it means the input matrix is not an identity matrix.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to find the largest element in the matr... >>
<< C# program to print the lower triangular matrix...