C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to determine whether a given matrix is an identity matrixExplanationIn this program, we need to check whether the given matrix is an identity matrix. Identity MatrixA matrix is said to be the identity matrix if it is the square matrix in which elements of principle diagonal are ones, and the rest of the elements are zeroes. Algorithm
SolutionPython#Initialize matrix a a = [ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]; flag = True; #Calculates number of rows and columns present in given matrix rows = len(a); cols = len(a[0]); #Checks whether given matrix is a square matrix or not if(rows != cols): print("Matrix should be a square matrix"); else: #Checks if diagonal elements are equal to 1 and rest of elements are 0 for i in range(0, rows): for j in range(0, cols): if(i == j and a[i][j] != 1): flag = False; break; if(i != j and a[i][j] != 0): flag = False; break; if(flag): print("Given matrix is an identity matrix"); else: print("Given matrix is not an identity matrix"); Output: Given matrix is an identity matrix C#include <stdio.h> #include <stdbool.h> int main() { int rows, cols; bool flag = true; //Initialize matrix a int a[][3] = { {1, 0, 0}, {0, 1, 0}, {0, 0, 1} }; //Calculates number of rows and columns present in given matrix rows = (sizeof(a)/sizeof(a[0])); cols = (sizeof(a)/sizeof(a[0][0]))/rows; //Checks whether given matrix is a square matrix or not if(rows != cols){ printf("Matrix should be a square matrix"); } else{ //Checks if diagonal elements are equal to 1 and rest of elements are 0 for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(i == j && a[i][j] != 1){ flag = false; break; } if(i != j && a[i][j] != 0){ flag = false; break; } } } if(flag) printf("Given matrix is an identity matrix"); else printf("Given matrix is not an identity matrix"); } return 0; } Output: Given matrix is an identity matrix JAVApublic class IdentityMatrix { public static void main(String[] args) { int rows, cols; boolean flag = true; //Initialize matrix a int a[][] = { {1, 0, 0}, {0, 1, 0}, {0, 0, 1} }; //Calculates the number of rows and columns present in the given matrix rows = a.length; cols = a[0].length; //Checks whether given matrix is a square matrix or not if(rows != cols){ System.out.println("Matrix should be a square matrix"); } else { //Checks if diagonal elements are equal to 1 and rest of elements are 0 for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(i == j && a[i][j] != 1){ flag = false; break; } if(i != j && a[i][j] != 0){ flag = false; break; } } } if(flag) System.out.println("Given matrix is an identity matrix"); else System.out.println("Given matrix is not an identity matrix"); } } } Output: Given matrix is an identity matrix C#using System; public class IdentityMatrix { public static void Main() { int rows, cols; Boolean flag = true; //Initialize matrix a int[,] a = { {1, 0, 0}, {0, 1, 0}, {0, 0, 1} }; //Calculates the number of rows and columns present in the given matrix rows = a.GetLength(0); cols = a.GetLength(1); //Checks whether given matrix is a square matrix or not if(rows != cols){ Console.WriteLine("Matrix should be a square matrix"); } else { //Checks if diagonal elements are equal to 1 and rest of elements are 0 for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(i == j && a[i,j] != 1){ flag = false; break; } if(i != j && a[i,j] != 0){ flag = false; break; } } } if(flag) Console.WriteLine("Given matrix is an identity matrix"); else Console.WriteLine("Given matrix is not an identity matrix"); } } } Output: Given matrix is an identity matrix PHP<!DOCTYPE html> <html> <body> <?php //Initialize matrix a $a = array( array(1, 0, 0), array(0, 1, 0), array(0, 0, 1) ); $flag = true; //Calculates number of rows and columns present in given matrix $rows = count($a); $cols = count($a[0]); //Checks whether given matrix is a square matrix or not if($rows != $cols){ print("Matrix should be a square matrix <br>"); } else { //Checks if diagonal elements are equal to 1 and rest of elements are 0 for($i = 0; $i < $rows; $i++){ for($j = 0; $j < $cols; $j++){ if($i == $j && $a[$i][$j] != 1){ $flag = false; break; } if($i != $j && $a[$i][$j] != 0){ $flag = false; break; } } } if($flag) print("Given matrix is an identity matrix <br>"); else print("Given matrix is not an identity matrix <br>"); } ?> </body> </html> Output: Given matrix is an identity matrix
Next Topic#
|