C Program To Check whether Matrix is Skew Symmetric or not

A Skew Symmetric Matrix or Anti-Symmetric Matrix is a square matrix whose transpose is negative to that of the original matrix. If the entry in the ith row and jth column of a matrix is a[i][j], i.e. if A = (a[i][j]) then the skew symmetric condition is -A = -a[j][i].
Examples :
Input : matrix: 0 5 -4
-5 0 1
4 -1 0
Output:
Transpose matrix: 0 -5 4
5 0 -1
-4 1 0
Skew Symmetric matrix
Approach:
- Find the transpose of the input matrix.
- If the input matrix is equal to the negative of its transpose matrix, then the matrix is Skew Symmetrical.
Below is the implementation of the above approach:
Java
// java program to check// whether given matrix// is skew-symmetric or notimport java.io.*;class GFG { static int ROW =3;static int COL =3;// Utility function to create transpose matrix static void transpose(int transpose_matrix[][], int matrix[][]){for (int i = 0; i < ROW; i++) for (int j = 0; j < COL; j++) transpose_matrix[j][i] = matrix[i][j];}// Utility function to check skew - symmetric// matrix condition static boolean check(int transpose_matrix[][], int matrix[][]){ for (int i = 0; i < ROW; i++) for (int j = 0; j < COL; j++) if (matrix[i][j] != -transpose_matrix[i][j]) return false; return true;}// Utility function to print a matrix static void printMatrix(int matrix[][]){ for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) System.out.print(matrix[i][j] + " "); System.out.println(); }}// Driver program to test above functionspublic static void main (String[] args) { int matrix[][] = { {0, 5, -4}, {-5, 0, 1}, {4, -1, 0}, }; int transpose_matrix[][] = new int[ROW][COL]; // Function create transpose matrix transpose(transpose_matrix, matrix); System.out.println ("Transpose matrix: "); printMatrix(transpose_matrix); // Check whether matrix is skew-symmetric or not if (check(transpose_matrix, matrix)) System.out.println("Skew Symmetric Matrix"); else System.out.println("Not Skew Symmetric Matrix"); }}// This code is contributed by vt_m. |
C++
// C program to check whether given matrix// is skew-symmetric or not#include <stdio.h>#include <stdlib.h>#define ROW 3#define COL 3// Utility function to create transpose matrixvoid transpose(int transpose_matrix[ROW][COL], int matrix[ROW][COL]){ for (int i = 0; i < ROW; i++) for (int j = 0; j < COL; j++) transpose_matrix[j][i] = matrix[i][j];}// Utility function to check skew - symmetric// matrix conditionbool check(int transpose_matrix[ROW][COL], int matrix[ROW][COL]){ for (int i = 0; i < ROW; i++) for (int j = 0; j < COL; j++) if (matrix[i][j] != -transpose_matrix[i][j]) return false; return true;}// Utility function to print a matrixvoid printMatrix(int matrix[ROW][COL]){ for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) printf("%d ", matrix[i][j]); printf("\n"); }}// Driver program to test above functionsint main(){ int matrix[ROW][COL] = { {0, 5, -4}, {-5, 0, 1}, {4, -1, 0}, }; int transpose_matrix[ROW][COL]; // Function create transpose matrix transpose(transpose_matrix, matrix); printf ("Transpose matrix: \n"); printMatrix(transpose_matrix); // Check whether matrix is skew-symmetric or not if (check(transpose_matrix, matrix)) printf("Skew Symmetric Matrix"); else printf("Not Skew Symmetric Matrix"); return 0;} |
C#
// C# program to check// whether given matrix// is skew-symmetric or notusing System;class GFG {static int ROW =3;static int COL =3;// Utility function to// create transpose matrixstatic void transpose(int [,]transpose_matrix, int [,]matrix){for (int i = 0; i < ROW; i++) for (int j = 0; j < COL; j++) transpose_matrix[j,i] = matrix[i,j];}// Utility function to check // skew - symmetric matrix // conditionstatic bool check(int [,]transpose_matrix, int [,]matrix){ for (int i = 0; i < ROW; i++) for (int j = 0; j < COL; j++) if (matrix[i, j] != -transpose_matrix[i, j]) return false; return true;}// Utility function// to print a matrixstatic void printMatrix(int [,]matrix){ for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) Console.Write(matrix[i, j] + " "); Console.WriteLine(); }}// Driver Codepublic static void Main () { int [,]matrix = {{0, 5, -4}, {-5, 0, 1}, {4, -1, 0},}; int [,]transpose_matrix = new int[ROW, COL]; // Function create transpose matrix transpose(transpose_matrix, matrix); Console.WriteLine("Transpose matrix: "); printMatrix(transpose_matrix); // Check whether matrix is // skew-symmetric or not if (check(transpose_matrix, matrix)) Console.WriteLine("Skew Symmetric Matrix"); else Console.WriteLine("Not Skew Symmetric Matrix"); }}// This code is contributed by anuj_67. |
Python3
# Python 3 program to check# whether given matrix# is skew-symmetric or notROW=3COL=3# Utility function to# create transpose matrixdef transpose(transpose_matrix,matrix): for i in range (ROW): for j in range(COL): transpose_matrix[j][i] = matrix[i][j] # Utility function to# check skew - symmetric# matrix conditiondef check(transpose_matrix,matrix): for i in range(ROW): for j in range(COL): if (matrix[i][j] != -transpose_matrix[i][j]): return False return True # Utility function to print a matrixdef printMatrix(matrix): for i in range (ROW): for j in range(COL): print(matrix[i][j]," ",end="") print() # Driver program to test above functionsmatrix= [ [0, 5, -4], [-5, 0, 1], [4, -1, 0], ]transpose_matrix=[[0 for i in range(3)] for j in range(3)]# Function create transpose matrixtranspose(transpose_matrix, matrix)print("Transpose matrix:")printMatrix(transpose_matrix)# Check whether matrix is# skew-symmetric or notif (check(transpose_matrix, matrix)): print("Skew Symmetric Matrix")else: print("Not Skew Symmetric Matrix")# This code is contributed# by Azkia Anam. |
Javascript
// Javascript Program to check whether given matrix is skew-symmetric or notvar n = 3, m = 3;var matrix = [[ 0, 5, -4 ], [ -5, 0, 1 ], [ 4, -1, 0 ]];var transpose_matrix = [[], [], []];var transpose=function(transpose_matrix, matrix){ for (var i = 0; i < n; i++) for (var j = 0; j < m; j++) transpose_matrix[j][i] = matrix[i][j];}// Utility function to check skew - symmetric// matrix conditionvar check=function(transpose_matrix,matrix){ for (var i = 0; i < n; i++) for (var j = 0; j < m; j++) if (matrix[i][j] != -transpose_matrix[i][j]) return false; return true;}// Utility function to print a matrixvar printMatrix=function(matrix){ for (var i = 0; i < n; i++) { for (var j = 0; j < m; j++) console.log(matrix[i][j]); console.log('\n'); }}transpose(transpose_matrix, matrix);console.log("Transpose matrix: \n");printMatrix(transpose_matrix); // Check whether matrix is skew-symmetric or notif (check(transpose_matrix, matrix)) console.log("Skew Symmetric Matrix");else console.log("Not Skew Symmetric Matrix");// This code is contributed by Sajal Aggarwal. |
Transpose matrix: 0 -5 4 5 0 -1 -4 1 0 Skew Symmetric Matrix
Time complexity: O(ROW x COL).
Auxiliary Space: O(ROW x COL).
References : Wikipedia
This article is contributed by Aarti_Rathi and Akash Gupta. If you like zambiatek and would like to contribute, you can also write an article using write.zambiatek.com or mail your article to review-team@zambiatek.com. See your article appearing on the zambiatek main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



