C Program to Check Identity Matrix

C program to check ide tity matrix. Write a C program to check if given matrix is identity matrix. This C program accepts a matrix and checks if it is an identity matrix using for loop construct in c programming language.

An identity matrix is one which has all diagonal values equal to 1 and no diagonal values equal to 0.

For example,
 1  0  0
 0  1  0
 0  0  1

is an identity matrix.

C Program to Check Identity Matrix

/* Aim: Write a C program to check whether the matrix is identity matrix or not */

#include<stdio.h>

int main()
{
	int arr[2][2];
	int i,j,LED=1;
 
	for(i=0;i<=1;i++)
	{
		for(j=0;j<=1;j++)
		{
		printf("\n Enter value of A[%d][%d]= ",i,j);
		scanf("%d",&arr[i][j]);
		}
	}

	for(i=0;i<=1;i++)
	{
		for(j=0;j<=1;j++)
		{
			if(arr[i][j]!=1 && arr[j][i]!=0)
			{
			LED=0;  
			break;
			}  
		}
	}
 
	if(LED==1)
		printf("\n Given matrix is a identity matrix \n \n"); 
	else
		printf("\n Given matrix is not a identity matrix \n \n");

	 return 0;
}

Output:

 Enter value of A[0][0]= 1

 Enter value of A[0][1]= 0

 Enter value of A[1][0]= 0

 Enter value of A[1][1]= 1

 Given matrix is a identity matrix

Comments