-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwapUpperToLowerDiadonal.java
More file actions
67 lines (54 loc) · 1.14 KB
/
SwapUpperToLowerDiadonal.java
File metadata and controls
67 lines (54 loc) · 1.14 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 SwapUpperToLowerDiadonal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m,n;
System.out.println("Enter the row and column Size : ");
n = sc.nextInt();
int[][] a = new int[n][n];
//read the matrix elements
System.out.println("Enter the Matrix Elements : ");
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
{
a[i][j] = sc.nextInt();
}
}
//swap the elements in the matrix
for(int i = 0;i<n;i++)
{
for(int j = i+1;j<n;j++)
{
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
//printing the matrix elements
System.out.println("Elements of Matrix After Swaping Diagonally : ");
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
/*
Output :
Enter the row and column Size :
4
Enter the Matrix Elements :
1 2 3 4
5 6 7 8
9 8 7 4
6 5 3 2
Elements of Matrix After Swaping Diagonally :
1 5 9 6
2 6 8 5
3 7 7 3
*/