-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixTranspose.java
57 lines (46 loc) · 1.26 KB
/
MatrixTranspose.java
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
import java.util.*;
public class MatrixTranspose
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Dimensions of Matrices: ");
int n = sc.nextInt();
int arr[][] = new int[n][n];
System.out.println();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print("Enter element: ");
arr[i][j] = sc.nextInt();
}
}
System.out.println();
MatrixTranspose trans = new MatrixTranspose();
trans.transpose(arr);
sc.close();
}
public void transpose(int arr[][])
{
int temp;
for(int i = 0; i < arr.length; i++)
{
for(int j = i; j < arr.length; j++)
{
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
System.out.println("Transpose Matrix: ");
for(int i = 0; i < arr.length; i++)
{
for(int j = 0; j < arr.length; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}