You are here: Home / Topics / Program to find transpose of matrix without using another metrix in Java

Program to find transpose of matrix without using another metrix in Java

Filed under: Java on 2023-08-23 06:53:04

//  Program to find transpose of the Matrix without 
//  using second Matrix

class  MatrixTransSame
{
public static void main( String args[ ] )
{
 int  i, j;
 int  temp;  
 int  a[ ][ ] =  {   { 1, 2, 3 },
     { 4, 5, 6 },
     { 7, 8, 9 }  };
  
 System.out.println( " The Actual of matrices is : " );
 for( i=0 ; i<3 ; i++ )
 {
  for( j=0 ; j<3 ; j++ )
  {
   System.out.print( "    " + a[ i ][ j ] );
  }
  System.out.println();
 }  

 for( i=0 ; i<3 ; i++ )
 {
  for( j=0 ; j<3 ; j++ )
  {
   if( i <= j )
   {
    temp = a[ i ][ j ];
    a[ i ][ j ] = a[ j ][ i ];
    a[ j ][ i ] = temp;
   }
  }
 }

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


Output:

The Actual of matrices is : 
 1   2   3
 4   5   6
 7   8   9
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.