TASK-
Write a C program To determine whether a matrix is identity or Not!!
IDENTITY MATRIX: A matrix having 1 on diagonal entries and 0 on non diagonal entries.
CODE:
#include<stdio.h>
int main(){
int r,c,p1=0,p2=0,d,x;
printf("Enter the Dimension of matrix\n");
scanf("%d",&d);
int n[d][d];
printf("Enter the entries \n");
for (r=0;r<d;r++)
{
for (c=0;c<d;c++)
{
printf("Enter entry [%d %d]",r+1,c+1);
scanf("%d",&n[r][c]);
printf("\n");
}
}
for (r=0;r<d;r++)
{
for (c=0;c<d;c++)
{
if (r==c)
{
if (n[r][c]==1)
p1++;
}
if (r!=c)
{
if (n[r][c]==0)
p2++;
}
}
}
x=(d-1)*d; //There would be (d-1)*d zeros in an identity matrix where d is dimension
if (p1==d&&p2==x)
printf("Its an identity matrix");
else
printf("Its not an identity matrix");
}
EXPECTED OUTPUT:
Sir thanks for teaching I like ❤️
ReplyDelete