Matrix Transpose In Java

Today you will learn matrix transpose in java.

Here in this program elements entered by the user(input) will be in form of an array. Transpose of matrix is obtained by exchanging all the rows and columns of matrix with the help of for loop.

example: Matrix Transpose In Java

Matrix Transpose In Java

Also Read – Linear Search In Java

import java.util.Scanner;

public class Matrix_Transpose_In_Java {

public static void main(String[] args) {

int x, y, a, b;

Scanner sc = new Scanner(System.in);
System.out.println("Please enter the number of rows and columns of matrix: ");
x = sc.nextInt();
y = sc.nextInt();

int matrix_Transpose[][] = new int[x][y];
System.out.println("Please enter the elements of matrix: ");
for(a = 0;a < x;a++)
for(b = 0;b < y;b++)
matrix_Transpose[a][b] = sc.nextInt();

int transpose[][] = new int[y][x];
for(a = 0;a < x;a++)
{
for(b = 0;b < y;b++)
transpose[b][a] = matrix_Transpose[a][b];
}

System.out.println("Transposed Matrix: ");
for(a = 0;a < y;a++)
{
for(b = 0;b < x;b++)
System.out.print(transpose[a][b] + "\t");
System.out.print("\n");
}
}
}

Output :

Please enter the number of rows and columns of matrix: 2 3
Please enter the elements of matrix:
2 4 6
1 3 5
Transposed Matrix:
2    1
4    3
6    5

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *