
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program for Identity Matrix in C
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[3][3] = { {1, 0, 0}, {0, 1, 0}, {0, 0, 1}} Output: yes Input: m[3][3] == { {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
- Related Questions & Answers
- Python Program to Print an Identity Matrix
- Golang Program to Print an Identity Matrix
- C Program for sparse matrix
- Program for Markov matrix in C++
- C Program for Matrix Chain Multiplication
- How to create an identity matrix using Numpy?
- Construct an identity matrix of order n in JavaScript
- A C++ Program for 2d matrix for taking Transpose
- Python PyTorch – How to create an Identity Matrix?
- What are the identity rules for regular expression?
- Euler's Four Square Identity in C++
- Diagonally Dominant Matrix in C++ Program
- Program to check diagonal matrix and scalar matrix in C++
- What is Identity Theft?
- Program to check idempotent matrix in C++
Advertisements