# C program to check a given matrix is an identity matrix or not

Here, we are going to learn how to check whether a given matrix is an identity matrix or not in C programming language?
Submitted by Nidhi, on July 13, 2021

Problem Solution:

The identity matrix of size n is the n x n square matrix with ones on the main diagonal and zeros elsewhere.

Given a matrix, we have to check whether the matrix is an identity matrix or not using C program.

Program:

The source code to check a given matrix is an identity matrix or not is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

```// C program to check a given matrix is an identity matrix or not

#include <stdio.h>

int isIdentityMatrix(int Matrix[])
{
int flag = 0;
int i, j;

for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (i == j && Matrix[i][j] != 1) {
flag = -1;
break;
}
else if (i != j && Matrix[i][j] != 0) {
flag = -1;
break;
}
}
}

return flag;
}
int main(void)
{
int Matrix1 = {
{ 1, 0, 0 },
{ 0, 1, 0 },
{ 0, 0, 1 }
};

int Matrix2 = {
{ 1, 0, 0 },
{ 0, 1, 1 },
{ 0, 0, 1 }
};

if (isIdentityMatrix(Matrix1) == 0)
printf("Matrix1 is an IDENTITY MATRIX\n");
else
printf("Matrix1 is NOT an identity matrix\n");

if (isIdentityMatrix(Matrix2) == 0)
printf("Matrix2 is an IDENTITY MATRIX\n");
else
printf("Matrix2 is NOT an identity matrix\n");

return 0;
}
```

Output:

```Matrix1 is an IDENTITY MATRIX
Matrix2 is NOT an identity matrix
```

Explanation:

In the above program, we created two functions isIdentityMatrix() and main() function. The isIdentityMatrix() function is a user defined function, it is used to check a matrix is identity matrix or not.

In the main() function, we created two matrices Matrix1, Matrix2. Then we checked matrices are identity matrices or not using the isIdentityMatrix() function and then we printed the appropriate message on the console screen.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7176285,"math_prob":0.998141,"size":2056,"snap":"2021-31-2021-39","text_gpt3_token_len":570,"char_repetition_ratio":0.18615985,"word_repetition_ratio":0.15902965,"special_character_ratio":0.30593386,"punctuation_ratio":0.14769976,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99703795,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-26T13:43:45Z\",\"WARC-Record-ID\":\"<urn:uuid:958addbe-a66d-415b-9ce7-3c17805e3ebc>\",\"Content-Length\":\"113577\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:03ef3f65-e5f5-45ef-ba75-e566420fe5e6>\",\"WARC-Concurrent-To\":\"<urn:uuid:975911dc-9851-44f2-8583-49193586d338>\",\"WARC-IP-Address\":\"104.21.9.18\",\"WARC-Target-URI\":\"https://www.includehelp.com/c-programs/check-a-given-matrix-is-an-identity-matrix-or-not.aspx\",\"WARC-Payload-Digest\":\"sha1:ESY5EV4WOXNLZ36ASCAHRE3M7V2QRPEL\",\"WARC-Block-Digest\":\"sha1:MV3BIFKHIFR5I66NZBVSIOQQQORH3WUL\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046152129.33_warc_CC-MAIN-20210726120442-20210726150442-00518.warc.gz\"}"}