Problem Statement

You’re given a matrix mat[][]. You need to find and print the transpose of the matrix.

Examples:

How to Find the Transpose of a Rectangular Matrix

The order of the transpose of a rectangular matrix is opposite to that of the original matrix. For example, if the order of the original matrix is 3 x 4, therefore the order of the transpose of this matrix would be 4 x 3. Store every column of the original matrix as rows in the transposed matrix i. e. , transposeMatrix[i][j] = mat[j][i].

C++ Program to Find the Transpose of a Rectangular Matrix

Below is the C++ program to find the transpose of a rectangular matrix:

Output:

Python Program to Find the Transpose of a Rectangular Matrix

Below is the Python program to find the transpose of a rectangular matrix:

Output:

JavaScript Program to Find the Transpose of a Rectangular Matrix

Below is the JavaScript program to find the transpose of a rectangular matrix:

Output:

C Program to Find the Transpose of a Rectangular Matrix

Below is the C program to find the transpose of a rectangular matrix:

Output:

How to Find the Transpose of a Square Matrix

The order of transpose of a square matrix is the same as that of the original matrix. For example, if the order of the original matrix is 3 x 3, the order of the transpose of this matrix would still be 3 x 3. Thus, declare a matrix with the same order as that of the original matrix. Store every column of the original matrix as rows in the transposed matrix i. e. , transposeMatrix[i][j] = mat[j][i].

C++ Program to Find the Transpose of a Square Matrix

Below is the C++ program to find the transpose of a square matrix:

Output:

Python Program to Find the Transpose of a Square Matrix

Below is the Python program to find the transpose of a square matrix:

Output:

JavaScript Program to Find the Transpose of a Square Matrix

Below is the JavaScript program to find the transpose of a square matrix:

Output:

C Program to Find the Transpose of a Square Matrix

Below is the C program to find the transpose of a square matrix:

Output:

Solve Basic Programming Problems Based on Matrices

A matrix is a grid used to store or display data in a structured format. Matrices are widely used in programming to perform various operations. If you’re looking to cover all coding interview bases, you must know how to perform basic operations like addition, subtraction, multiplication, and more on matrices.