You are here: Home / Topics / Transpose of the array in Java

Transpose of the array in Java

Filed under: Java on 2023-08-23 06:51:24

//Program to find transpose of the Matrix

class  MatrixTranspose
{
public static void main( String args[ ] )
{
 int i, j;
 int  b[ ][ ] = new int[3][3];
 
 int   a[ ][ ] = {  
    {1, 2, 3},
    {4, 5, 6},
      {7, 8, 9}  };
 
 for( i=0 ; i<3 ; i++ )
 {
  for( j=0 ; j<3 ; j++ )
  {
   b[ i ][ j ] = a[ j ][ i ];
  }
 }

 System.out.println( " The Transpose of matrices is : " );
 for( i=0 ; i<3 ; i++ )
 {
  for( j=0 ; j<3 ; j++ )
  {
   System.out.print( "    " + b[ i ][ j ] );
  }
  System.out.println();
 }
}
}


Output:

The Transpose of matrices is : 
1   4   7
2   5   8
3   6   9

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.