Program to print transpose of matrix using without extra array in java.

class TransposemlMatrix
{
    public static void print(int[][] arr)
{
        int n=arr.length;
        for(int i=0; i<n; i++){
           for(int j=0; j<n; j++){
               System.out.print(arr[i][j]+" ");
           }
            System.out.println();
        }
    }

    public static void main(String[] args)
 {
       int [][] arr ={{1,2,3},{4,5,6},{7,8,9}};

       int n=arr.length;

       for(int i=0; i<n; i++)
{
           for(int j=0; j<i; j++)
          {
             int temp =arr[i][j];
             arr[i][j]=arr[j][i];
             arr[j][i]=temp;
           }
  }
       System.out.println("the transpose matrix is=");
       print(arr);
    }
}

■》Output->

the transpose matrix is=

1 4 7
2 5 8
3 6 9 

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.