Array programs collection C
/* to know a matrix of order n is identity or not
1 0 0
0 1 0
0 0 1
*/
#include<stdio.h>
int main()
{
int n,i,j,is_Identity=0;
printf("enter the size of matrix\n");
scanf("%d",&n);
int matrix[n][n];
printf("enter elements of given matrix\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%d",&matrix[i][j]);
}
}
for(i=0;i<=n-1;i++)//loop iteration for row
{
for(j=0;j<=n-1;j++)//loop iteration for column
{
if((i==j && matrix[i][j]!=1)||(i!=j && matrix[i][j]!=0))// checking the condition
// two conditons to bes tested
//Check if the current element is on the diagonal (i == j)
// In the identity matrix, diagonal elements must be 1
// Also check if the element is not on the diagonal (i != j)
// In identity matrix, non-diagonal elements must be 0
is_Identity=1;
break;
}
}
printf("the matrix is:\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%5d",matrix[i][j]);
}
printf("\n");
}
if(is_Identity==0)
{
printf("The matrix is identity");
}
else
{
printf("the matrix is not identity");
}
return 0;
}