Transpose of Matrix

This program finds the transpose of a matrix.

Problem Statement

Write a Java program to find the transpose of a 3x3 matrix.

Source Code

1 public class MatrixTranspose {
2 public static void main(String[] args) {
3 int[][] A = {{1,2,3},{4,5,6},{7,8,9}};
4 int[][] T = new int[3][3];
5 for(int i=0;i<3;i++){
6 for(int j=0;j<3;j++){
7 T[j][i] = A[i][j];
8 }
9 }
10 for(int i=0;i<3;i++){
11 for(int j=0;j<3;j++){
12 System.out.print(T[i][j]+" ");
13 }
14 System.out.println();
15 }
16 }
17 }

Program Output

1 4 7 
2 5 8 
3 6 9 

Explanation

Transpose swaps rows and columns. - Nested loops assign T[j][i] = A[i][j]. - Together demonstrates matrix transposition. - Shows how multidimensional arrays can be rearranged. Conclusion: Demonstrates transpose of a matrix using arrays.

Quick Links to Explore