C# Program to Check if a Matrix is an Identity Matrix

This is a C# Program to check if a given matrix is an identity matrix.

Problem Description

This C# Program Checks If a Given Matrix is an Identity Matrix.

Problem Solution

Here an identity matrix is a square matrix, of size n × n, where the diagonal elements are all 1s, and the other elements are all 0s.

Program/Source Code

Here is source code of the C# Program to Check If a Given Matrix is an Identity Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Check If a Given Matrix is an Identity Matrix
 */
using System;
class pro
{
    public static void Main()
    {
        Console.WriteLine("Enter the order: ");
        int n = int.Parse(Console.ReadLine());
        int[,] a = new int[3, 3];
        int i, j;
        Console.WriteLine("\n Enter the matrix\n");
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                a[i, j] = Convert.ToInt16(Console.ReadLine());
            }
        }
 
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                if ((i == j && a[i, j] != 1) || (i != j && a[i, j] != 0))
                {
                    goto label;
                }
            }              
        }
    Console.WriteLine("Identity Matrix");
    return;
label:
    Console.WriteLine("\n Not an Identity Matrix");
    }
}
Program Explanation

In this C# program, we are reading the order of the matrix using ‘n’ variable. Using for loop we are entering the elements for matrix. An identity matrix is a square matrix with 1’s along the diagonal from upper left to lower right and 0’s in all other positions.

advertisement
advertisement

If condition statement is used to check that the matrix element a[0][0], a[1][1], a[2][2] is one, remaining element values are zero using AND logical operator. If the condition is true, then print the statement as identity matrix. Otherwise, if the condition is false then execute the else statement and print as not identity matrix.

Runtime Test Cases
 
Enter the Order : 2
Enter the Matrix :
1
0
0
1
Identity Matrix

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.