Code in java to find the transpose of a matrix.

What is transpose of a matrix.

Table of contents

No heading

No headings in the article.

suppose your matrix A is 3x3 ok, now every element of your matrix is present at A(i,j)

So, let Transpose of your matrix is B in which row changed to the column that is A(i,j) element is going to B(j,i);

import java.util.Scanner;
public class Main {
    public static void main (String[] args) {
       Scanner sc = new Scanner(System.in);
//taking input from user the no. of row and column
         int r = sc.nextInt();
         int c = sc.nextInt();
       int[][] arr = new int[r][c];

       int[][] tran = new int[c][r];

//taking input from user in the matrix

       for(int i=0;i<arr.length;i++){
           for(int j=0; j<arr[i].length;j++){
                arr[i][j] = sc.nextInt();
           }
       }

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


//ading space
 System.out.println();
 System.out.println();

        //transpose of matrix
        for(int i=0;i<tran.length;i++){
            for(int j=0; j<tran[i].length;j++){
                tran[i][j] = arr[j][i];
            }
        }
        //printing the transpose matrix;

        for(int i=0;i<tran.length;i++){
            for(int j=0; j<tran[i].length;j++){
                System.out.print(" "+tran[i][j]+" ");
            }
            System.out.println();
        }
    }
}