-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransposeOf2DMatrix.java
More file actions
56 lines (46 loc) · 1.74 KB
/
Copy pathTransposeOf2DMatrix.java
File metadata and controls
56 lines (46 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Author : Hasnain Memon
* Date : 02/11/2024
* Question : Write a program that finds the transpose of a 2D matrix.
*/
import java.util.Scanner;
public class TransposeOf2DMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows, columns;
int[][] matrix;
System.out.print("Enter number of rows of a matrix: ");
rows = sc.nextInt();
System.out.print("Enter number of columns of a matrix: ");
columns = sc.nextInt();
matrix = new int[rows][columns];
for (int i = 0 ; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print("Enter element[" + i + "][" + j + "]: ");
matrix[i][j] = sc.nextInt();
}
System.out.println();
}
System.out.println("2D matrix of " + rows + " rows and " + columns + " columns");
for (int i = 0 ; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
int[][] transposeMatrix = new int[columns][rows];
//Finding Transpose of the matrix
for (int i = 0 ; i < rows; i++) {
for (int j = 0; j < columns; j++) {
transposeMatrix[j][i] = matrix[i][j];
}
}
System.out.println("Transpose of 2D matrix of " + columns + " rows and " + rows + " columns");
for (int i = 0 ; i < columns; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(transposeMatrix[i][j] + " ");
}
System.out.println();
}
}
}