Write a Java Program to find Transpose Matrix

By | 27.01.2017

Write a Java Program to find Transpose Matrix


To transpose matrix in Java Programming, first you have to ask to the user to enter the matrix elements.

Now, to transpose any matrix, you have to replace the row elements by the column elements and vice-versa.

  • Following Java Program ask to the user to enter the n*n array element to transpose and display the transpose of the Matrix on the screen:

 

SOURCE CODE ::

 

import java.util.Scanner;
 
public class Transpose_matrix {
   public static void main(String args[])
   {
      int m, n, c,d;
      Scanner in = new Scanner(System.in);
 
      System.out.println("Enter the number of rows and columns of matrix");
      m = in.nextInt();
      n = in.nextInt();
 
      int first[][] = new int[m][n];
 
      System.out.println("Enter the elements of matrix");
 
      for (  c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
         {
            System.out.print(c+""+d+" Element : ");
            first[c][d] = in.nextInt();
         }
 
     int transpose[][] = new int[n][m];
 
      for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < n ; d++ )               
            transpose[d][c] = first[c][d];
      }
 
      System.out.println("Transpose of entered matrix:-");
 
      for ( c = 0 ; c < n ; c++ )
      {
         for ( d = 0 ; d < m ; d++ )
               System.out.print(transpose[c][d]+"\t");
 
         System.out.print("\n");
      }
   }
}

 

OUTPUT ::

 

Enter the number of rows and columns of matrix
3
2
Enter the elements of matrix
00 Element : 1
01 Element : 2
10 Element : 3
11 Element : 4
20 Element : 5
21 Element : 6
Transpose of entered matrix:-
1       3       5       
2       4       6

 

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments