﻿ Example of C Program/Code to check if a given matrix is an identity matrix - EngineersGarage
Close or Esc Key

Check if a given matrix is an identity matrix

`/* Write a 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");}/*------------------------------------------OutputRun 1Enter the order of the matrix A2 2Enter the elements of matrix A2 21 2MATRIX A is  2  2  1  2It is not a identity matrixRun 2Enter the order of the matrix A2 2Enter the elements of matrix A1 00 1MATRIX A is  1  0  0  1It is identity matrix------------------------------------------*/`