C++ Program for Identity Matrix Last Updated : 28 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Introduction to Identity Matrix : The dictionary definition of an Identity Matrix is a square matrix in which all the elements of the principal or main diagonal are 1's and all other elements are zeros. In the below image, every matrix is an Identity Matrix. In linear algebra, this is sometimes called as a Unit Matrix, of a square matrix (size = n x n) with ones on the main diagonal and zeros elsewhere. The identity matrix is denoted by " I ". Sometimes U or E is also used to denote an Identity Matrix. A property of the identity matrix is that it leaves a matrix unchanged if it is multiplied by an Identity Matrix. Examples: Input : 2 Output : 1 0 0 1 Input : 4 Output : 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 The explanation is simple. We need to make all the elements of principal or main diagonal as 1 and everything else as 0. Program to print Identity Matrix : The logic is simple. You need to the print 1 in those positions where row is equal to column of a matrix and make all other positions as 0. C++ // C++ program to print Identity Matrix #include<bits/stdc++.h> using namespace std; int Identity(int num) { int row, col; for (row = 0; row < num; row++) { for (col = 0; col < num; col++) { // Checking if row is equal to column if (row == col) cout << 1 << " "; else cout << 0 << " "; } cout << endl; } return 0; } // Driver Code int main() { int size = 5; Identity(size); return 0; } // This code is contributed by shubhamsingh10 Output: 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 Time complexity: O(n^2) where n is no of rows and column in matrix Auxiliary Space: O(1) Program to check if a given square matrix is Identity Matrix : C++ // CPP program to check if a given matrix is identity #include<iostream> using namespace std; const int MAX = 100; bool isIdentity(int mat[][MAX], int N) { for (int row = 0; row < N; row++) { for (int col = 0; col < N; col++) { if (row == col && mat[row][col] != 1) return false; else if (row != col && mat[row][col] != 0) return false; } } return true; } // Driver Code int main() { int N = 4; int mat[][MAX] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; if (isIdentity(mat, N)) cout << "Yes "; else cout << "No "; return 0; } Output: Yes Time complexity: O(n^2) where n is no of rows and column in matrix Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article C++ Program for Identity Matrix kartik Follow Improve Article Tags : Mathematical Matrix C++ Programs C++ Computer Science Fundamentals DSA +2 More Practice Tags : CPPMathematicalMatrix Similar Reads C++ Program for Markov matrix Given a m x n 2D matrix, check if it is a Markov Matrix.Markov Matrix : The matrix in which the sum of each row is equal to 1. Example of Markov Matrix Examples: Input : 1 0 0 0.5 0 0.5 0 0 1 Output : yes Explanation : Sum of each row results to 1, therefore it is a Markov Matrix. Input : 1 0 0 0 0 2 min read C++ Program to check idempotent matrix Given N * N matrix and the task is to check matrix is an idempotent matrix or not.Idempotent matrix: A matrix is said to be an idempotent matrix if the matrix multiplied by itself returns the same matrix. The matrix M is said to be an idempotent matrix if and only if M * M = M. In an idempotent matr 2 min read Matrix C/C++ Programs C Program to check if two given matrices are identicalC program to find transpose of a matrixC program for subtraction of matricesC program for addition of two matricesC program to multiply two matricesC/C++ Program for Print a given matrix in spiral formC/C++ Program for A Boolean Matrix QuestionC/ 1 min read C++ Program to check Involutory Matrix Given a matrix and the task is to check matrix is an involutory matrix or not. Involutory Matrix: A matrix is said to be an involutory matrix if matrix multiply by itself returns the identity matrix. The involutory matrix is the matrix that is its own inverse. The matrix A is said to be an involutor 2 min read C++ Program For Determinant of a Matrix What is the Determinant of a Matrix? The determinant of a Matrix is a special number that is defined only for square matrices (matrices that have the same number of rows and columns). A determinant is used at many places in calculus and other matrices related to algebra, it actually represents the m 12 min read C++ Program to Implement Adjacency Matrix An adjacency matrix is a way to represent graph data structure in C++ in the form of a two-dimensional matrix. It is a simple square matrix of size V*V, where V represents the number of vertices in the graph. Each cell of the matrix represents an edge between the row vertex and column vertex of the 4 min read C++ Program for Diagonally Dominant Matrix In mathematics, a square matrix is said to be diagonally dominant if, for every row of the matrix, the magnitude of the diagonal entry in a row is larger than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row. More precisely, the matrix A is diagonally dominan 3 min read C++ Program to Print matrix in antispiral form Given a 2D array, the task is to print matrix in anti spiral form:Examples: Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Input : arr[][4] = {1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16}; Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12 3 min read C++ Program For Boundary Elements of a Matrix Printing Boundary Elements of a Matrix. Given a matrix of size n x m. Print the boundary elements of the matrix. Boundary elements are those elements which are not surrounded by elements on all four directions, i.e. elements in first row, first column, last row and last column. Examples: Input: 1 4 min read Like