 C Tutorial

# C program to generate identity matrix for required rows and columns

## C program to generate identity matrix for required rows and columns using two dimensional array

Identity Matrix is the matrix equivalent of the number 1 and represented by I always.

Identity matrix is a square and has same number of rows and columns, then all diagonal place value is 1's and remaining place 0's.

Same matrix is the result when any matrix multiplied by identity matrix.

Let us see example 3x3 identity matrix,

```1 0 0
0 1 0
0 0 1
```

This c program is used to generate the identity matrix using two dimensional array and nested for loops.

```#include<stdio.h>

#define COLUMN 10
#define ROW 10

void main()
{
int identity_matrix[ROW][COLUMN];
int i,j, rows, columns;

printf("\nEnter Matrix Rows: ");
scanf("%d", &rows);
printf("\nEnter Matrix Columns: ");
scanf("%d", &columns);

for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
if(i==j)
{
identity_matrix[i][j] = 1;
}
else
{
identity_matrix[i][j] = 0;
}
}
}
printf("\nIdentity Matrix: \n");
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
printf("%d\t", identity_matrix[i][j]);
}
printf("\n");
}
}
```

Output:

```\$ cc identity_matrix.c
\$ ./a.out

Enter Matrix Rows: 4

Enter Matrix Columns: 4

Identity Matrix:
1	0	0	0
0	1	0	0
0	0	1	0
0	0	0	1

\$ ./a.out

Enter Matrix Rows: 6

Enter Matrix Columns: 6

Identity Matrix:
1	0	0	0	0	0
0	1	0	0	0	0
0	0	1	0	0	0
0	0	0	1	0	0
0	0	0	0	1	0
0	0	0	0	0	1
``` C Tutorial

## C Examples

Privacy Policy  |  Copyright 2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page     