-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixRotation.java
More file actions
54 lines (51 loc) · 1.67 KB
/
Copy pathMatrixRotation.java
File metadata and controls
54 lines (51 loc) · 1.67 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
package basic;
import java.util.Scanner;
class MatrixRotation {
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
System.out.println("Enter order of square matrix ");
int M = ob.nextInt();
if (M <= 2 || M >= 10) {
System.out.println("Invalid Input, Enter order between 2 and 10");
System.exit(0);
}
int A[][] = new int[M][M];
System.out.println("Enter a matrix of order :" + M);
for (int i = 0; i < M; i++) //Input Matrix
{
for (int j = 0; j < M; j++) {
A[i][j] = ob.nextInt();
}
}
System.out.println("ORIGINAL MATRIX");
for (int i = 0; i < M; i++) //Printing original matrix
{
for (int j = 0; j < M; j++) {
System.out.print(A[i][j] + " ");
}
System.out.println("");
}
rotate(A, M);
}
public static void rotate(int A[][], int M) //Method to rotate the matrix by 90`
{
System.out.println("MATRIX AFTER ROTATION");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (i < j) {
int temp = A[j][i];
A[j][i] = A[i][j];
A[i][j] = temp;
}
}
}
for (int i = 0; i < M; i++) {
for (int j = M - 1; j >= 0; j--) {
System.out.print(A[i][j] + " ");
}
System.out.println("");
}
int sum = A[0][0] + A[0][M - 1] + A[M - 1][0] + A[M - 1][M - 1];
System.out.println("Sum of the corner elements = " + sum);
}
}