Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Java Program to transpose matrix

Converting rows of a matrix into columns and columns of a matrix into row is called transpose of a matrix.

Program 1: Display the Transpose of a Matrix

In this program, we will use a separate matrix to store the transpose elements.
Algorithm
1. Start
2. Declare variables for matrix rows and columns.
3. Ask the user to initialize the rows and columns.
4. Declare a matrix.
5. Ask the user to initialize the matrix elements.
6. Print the original matrix.
7. Declare another matrix that will store the transpose matrix element.
8. Store the elements in the transpose matrix by altering the rows and columns of
the original matrix.
9. Display the transpose matrix.
10. Stop.
public class transpose {

public static void main(String[] args) {


//creating a matrix
int original[][]={{5,4,3},{1,2,6},{9,8,7}};
//creating another matrix to store transpose of a
matrix
int transpose[][]=new int[3][3]; //3 rows and 3
columns
//Code to transpose a matrix
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=original[j][i];
}
}
System.out.println("Printing Matrix without transpose:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(original[i][j]+" ");
}
System.out.println();//new line
}
System.out.println("Printing Matrix After Transpose:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(transpose[i][j]+" ");
}
System.out.println();//new line
}
}
}
Output

Printing Matrix without transpose:


5 4 3
1 2 6
9 8 7
Printing Matrix After Transpose:
5 1 9
4 2 8
3 6 7

You might also like