-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranspose.java
More file actions
45 lines (44 loc) · 948 Bytes
/
Transpose.java
File metadata and controls
45 lines (44 loc) · 948 Bytes
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
package unit1;
import java.util.*;
public class Transpose {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter no of rows and columns of a matrix : ");
int r, c, i, j;
r = sc.nextInt();
c = sc.nextInt();
int[][] mat = new int[r][c];
System.out.println("Enter the elements of the matrix : ");
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
mat[i][j] = sc.nextInt();
}
}
System.out.println("The given matrix is : ");
display(mat, r, c);
int[][] tra = new int[c][r];
for(i = 0; i < c; i++)
{
for(j = 0; j < r; j++)
{
tra[i][j] = mat[j][i];
}
}
System.out.println("The transpose of given matrix is : ");
display(tra, c, r);
sc.close();
}
static void display(int a[][], int m, int n)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}