Java Program to Find Transpose of Matrix

Transpose of a matrix is a new matrix whose columns are the rows of original matrix and rows are the columns of original matrix. Take below example for transpose of matrix of order 3×3.

Java Program to Find Transpose of Matrix
Image Source

Below I have shared java program that will find transpose of matrix of any order. If you find any difficulty to understand then ask your queries in the comment section.

Java Program to Find Transpose of Matrix

import java.util.Scanner;

class MatrixTranspose
{
 public static void main(String...s)
 {
  int m,n,i,j;  

  Scanner sc=new Scanner(System.in);
  System.out.println("Enter number of rows and columns:");
  m=sc.nextInt();
  n=sc.nextInt();

  int arr[][]=new int[m][n],trn[][]=new int[n][m];

  System.out.println("nEnter elements of matrix row wise:");
  
  for(i=0;i<m;++i)
   for(j=0;j<n;++j)
    arr[i][j]=sc.nextInt();

  for(i=0;i<m;++i)
   for(j=0;j<n;++j)
    trn[j][i]=arr[i][j];


  System.out.println("nMatrix after transpose:");
  for(i=0;i<n;++i)
  {
   for(j=0;j<m;++j)
    System.out.print(trn[i][j]+" ");
   System.out.print("n");
  }
       
 }
}  

Java Program to Find Transpose of Matrix

1 thought on “Java Program to Find Transpose of Matrix”

Leave a Comment

Your email address will not be published. Required fields are marked *