C Program to check if a given matrix is an identity matrix

by Nideesh C on April 12, 2011 · 0 comments

in C Programming




/* C Program to check if a given matrix is an identity matrix */

#include <stdio.h>

void main()
{
int A[10][10];
int i, j, R, C, flag =1;

printf(“Enter the order of the matrix A\n”);
scanf(“%d %d”, &R, &C);

printf(“Enter the elements of matrix A\n”);
for(i=0; i<R; i++)
{
for(j=0; j<C; j++)
{
scanf(“%d”,&A[i][j]);
}
}
printf(“MATRIX A is\n”);
for(i=0; i<R; i++)
{
for(j=0; j<C; j++)
{
printf(“%3d”,A[i][j]);
}
printf(“\n”);
}

/* Check for unit (or identity) matrix */

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 identity matrix\n”);
else
printf(“It is not a identity matrix\n”);
}

/*——————————————
Output
Run 1
Enter the order of the matrix A
2 2
Enter the elements of matrix A
2 2
1 2
MATRIX A is
2  2
1  2
It is not a identity matrix

Run 2
Enter the order of the matrix A
2 2
Enter the elements of matrix A
1 0
0 1
MATRIX A is
1  0
0  1
It is identity matrix
——————————————*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C Program to accept two matrices and check if they are equal
  2. C program to read A (MxN), find the transpose of a given matrix and output both the input matrix and the transposed matrix.
  3. C program to read two matrices A (MxN) and B(MxN) and perform addition OR subtraction of A and B. Find the trace of the resultant matrix. Output the given matrix, their sum or Differences and the trace.
  4. C program to read a matrix A (MxN) and to find the following using functions a) Sum of the elements of each row b) Sum of the elements of each column c) Find the sum of all the elements of the matrix Output the computed results with suitable headings
  5. C program to accept two matrices and find the sum and difference of the matrices

Leave a Comment

Previous post:

Next post: