Java program to find transpose of matrix


Program
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of row"); int row=sc.nextInt(); System.out.println("Enter the number of column"); int col=sc.nextInt(); int arr1[][]=new int[row][col]; int transpose[][]=new int[col][row]; System.out.println("Enter array elements"); for(int i=0;i<row;i++) { for(int j=0;j<col;j++){ arr1[i][j]=sc.nextInt(); } } System.out.println("Matrix enter by user"); for(int i=0;i<row;i++) { for(int j=0;j<col;j++){ System.out.print(arr1[i][j]+" "); } System.out.println(); } for(int i=0;i<row;i++) { for(int j=0;j<col;j++){ transpose[j][i]=arr1[i][j]; } } System.out.println("Transpose of matrix"); for(int i=0;i<col;i++) { for(int j=0;j<row;j++){ System.out.print(transpose[i][j]+" "); } System.out.println(); } } }
Input
Output