# Program for Identity Matrix in C

CServer Side ProgrammingProgramming

Given a square matrix M[r][c] where ‘r’ is some number of rows and ‘c’ are columns such that r = c, we have to check that ‘M’ is identity matrix or not.

## Identity Matrix

Identity matrix is also known as Unit matrix of size nxn square matrix where diagonal elements will only have integer value one and non diagonal elements will only have integer value as 0

Like in the given Example below −

$$I1=\begin{bmatrix}1 \end{bmatrix},\\ I2=\begin{bmatrix}1 & 0 \\0 & 1 \end{bmatrix},\\ I3=\begin{bmatrix}1 &0 & 0 \\0 &1 & 0 \\0 &0 &1 \end{bmatrix},\\In=\begin{bmatrix} 1 &0 &0 &...&0 \\ 0 &1 &0 &...&0\\ 0 &0 &1 &...&0\\ . &. &. &...&.\\ . &. &. &...&.\\ 0 &0 &0 &...&1\\ \end{bmatrix}$$

## Example

Input: m = { {1, 0, 0},
{0, 1, 0},
{0, 0, 1}}
Output: yes
Input: m == { {3, 0, 1},
{6, 2, 0},
{7, 5, 3} }
Output: no

## Algorithm

Start
Step 1 -> declare function for finding identity matrix
int identity(int num)
declare int row, col
Loop For row = 0 and row < num and row++
Loop For col = 0 and col < num and col++
IF (row = col)
Print 1
Else
Print 0
End
End
Step 2 -> In main()
Declare int size = 4
Call identity(size)
Stop

## Example

#include<stdio.h>
int identity(int num){
int row, col;
for (row = 0; row < num; row++){
for (col = 0; col < num; col++){
if (row == col)
printf("%d ", 1);
else
printf("%d ", 0);
}
printf("\n");
}
return 0;
}
int main(){
int size = 4;
identity(size);
return 0;
}

## Output

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1