Write a program to check whether the matrix is an identity matrix or not in c
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],i,r,c,j,sum=0,flag=1;
clrscr();
printf("Enter the order of matrix: \n");
scanf("%d%d",&r,&c);
if(r==c)
{
printf("Enter the elements\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
printf("Entered Matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j]!=1&&a[j][i]!=0)
{
flag=0;
break;
}
}
}
if(flag==1)
printf("It
is an identity matrix");
else
printf("It
is not an identity matrix");
}
else
printf("It is not a square matrix to find identity
matrix");
getch();
}
Output:
run 1:
Enter the order of matrix:
2
2
Enter the elements
1 0
0 1
Entered Matrix is
1 0
0 1
It is an identity matrix
run 2:
Enter the order of matrix:
2
2
Enter the elements
1 2
0 1
Entered Matrix is
1 2
0 1
It is not an identity matrix
run 3:
Enter the order of matrix:
2
3
It is not a square matrix to find identity matrix
if it has a line ( 1 0 1 ), It will return true
ReplyDeleteIs it wrong ?
Yes!
Delete