-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransposeMatrix.java
More file actions
67 lines (52 loc) · 1.1 KB
/
TransposeMatrix.java
File metadata and controls
67 lines (52 loc) · 1.1 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
57
58
59
60
61
62
63
64
65
66
67
package matrices;
import java.util.Scanner;
public class TransposeMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m,n;
System.out.println("Enter the row and column Size : ");
m = sc.nextInt();
n = sc.nextInt();
int[][] a = new int[m][n];
int[][] b = new int[n][m];
//read the matrix elements
System.out.println("Enter the Matrix Elements : ");
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
{
a[i][j] = sc.nextInt();
}
}
//below lines are to transpose a matrix
for(int i = 0;i<n;i++)
{
for(int j = 0; j<m;j++)
{
b[i][j] = a[j][i];
}
}
//these are to display a matrix
System.out.println("The Transpose of the Matrix is : ");
for(int i = 0;i<n;i++)
{
for(int j = 0;j<m;j++)
{
System.out.print(b[i][j] +" ");
}
System.out.println();
}
}
}
/*
Output :
Enter the row and column Size :
2 3
Enter the Matrix Elements :
1 2 3
4 5 6
The Transpose of the Matrix is :
1 4
2 5
3 6
*/