**Transpose of a Matrix in Java** is the new matrix formed by interchanging its rows and columns. The transpose of the matrix is denoted by using the letter “T” in the superscript of the given matrix.

For example, if “A” is the given matrix, then the transpose of the matrix is represented by A’ or A^{T}.

**Example 1: **

Suppose we have a 3×3 matrix A:

\begin{bmatrix}

1 & 2 & 3\\

4 & 5 & 6\\

7 & 8 & 9

\end{bmatrix}\)

To find the transpose of A, we simply interchange its rows and columns to obtain a new 3×3 matrix.

A^{T} = \(
\begin{bmatrix}

1 & 4 & 7\\

2 & 5 & 8\\

3 & 6 & 9

\end{bmatrix}\)

Here, the first row of A becomes the first column of A^{T}, the second row of A becomes the second column of A^{T}, and so on.

**Example 2: **

If the order of the original matrix is 2×3, the order of the transposed matrix will be 3×2, as shown below.

If the given matrix \( A =

\begin{bmatrix}

1 & 2 & 3\\

4 & 5 & 6

\end{bmatrix}\), then the transpose matrix **(A ^{T})** is \(
\begin{bmatrix}

1 & 4 \\

2 & 5\\

3 & 6

\end{bmatrix}\)

Write a Java Program to find the transpose of a given matrix.

The transpose of a given matrix is calculated by interchanging the rows and columns of a matrix with the help of loops.

Here is the source code of the Java Program to Display Transpose Matrix. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import java.util.Scanner;

public class Transpose

`{`

public static void main(String args[])

`{`

int i, j;

System.out.println("Enter total rows and columns: ");

Scanner s = new Scanner(System.in);

int row = s.nextInt();

int column = s.nextInt();

int array[][] = new int[row][column];

System.out.println("Enter matrix:");

for(i = 0; i < row; i++)

`{`

for(j = 0; j < column; j++)

`{`

array[i][j] = s.nextInt();

System.out.print(" ");

`}`

`}`

System.out.println("The above matrix before Transpose is ");

for(i = 0; i < row; i++)

`{`

for(j = 0; j < column; j++)

`{`

System.out.print(array[i][j]+" ");

`}`

System.out.println(" ");

`}`

System.out.println("The above matrix after Transpose is ");

for(i = 0; i < column; i++)

`{`

for(j = 0; j < row; j++)

`{`

System.out.print(array[j][i]+" ");

`}`

System.out.println(" ");

`}`

`}`

`}`

1. Import Scanner class from java.util package.

2. Define a public class called **Transpose**.

3. Define a main method inside the class with String array as an argument.

4. Declare and initialize variables **i**, **j** to **0**.

5. Print a message to the console to enter the total rows and columns of the matrix.

6. Create an object of the **Scanner class** and read the user input of rows and columns.

7. Create a 2D array of integer type using the input dimensions.

8. Print a message to the console to enter the elements of the matrix.

9. Use nested for loops to read user input and store them in the matrix.

10. Print the matrix before transpose.

11. Use nested for loops to calculate the transpose of the matrix and print it to the console.

**Time Complexity: O(m*n)**

In the program, we are iterating over the complete matrix, so time complexity is O(m*n), where m, n are the rows and columns of the matrix.

**Space Complexity: O(m*n)**

Space is required to store the transpose of matrix, which is O(m*n), where m, n are the rows and columns of the matrix.

In this case, we enter the order of the matrix as “3 * 3” and the matrix elements as input to find the transpose of a matrix.

$ javac Transpose.java $ java Transpose Enter total rows and columns: 3 3 Enter matrix: 1 2 3 4 5 6 7 8 9 The above matrix before Transpose is 1 2 3 4 5 6 7 8 9 The above matrix after Transpose is 1 4 7 2 5 8 3 6 9

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

**Next Steps:**

- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Practice Programming MCQs
- Buy Programming Books
- Buy Java Books
- Practice BCA MCQs
- Practice Information Technology MCQs