-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateTheMatrix.java
More file actions
52 lines (52 loc) · 999 Bytes
/
RotateTheMatrix.java
File metadata and controls
52 lines (52 loc) · 999 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
46
47
48
49
50
51
52
/*Write a program that takes a NxN square matrix as an input and rotates it clockwise by 90 degrees.
Input:
1 2 3
4 5 6
7 8 9
Output:
7 4 1
8 5 2
9 6 3
*/
import java.util.*;
public class RotateTheMatrix{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("Enter 3x3 Matrix");
int a[][]=new int[3][3];
int b[][]=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=in.nextInt();
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i][j]=0;
}
}
b[0][2]=a[0][0];
b[1][2]=a[0][1];
b[2][2]=a[0][2];
b[0][1]=a[1][0];
b[1][1]=a[1][1];
b[2][1]=a[1][2];
b[0][0]=a[2][0];
b[1][0]=a[2][1];
b[2][0]=a[2][2];
System.out.println("The Matrix After 90deg rotation is:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(b[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
}