forked from ravitomar7/Lab_Test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
52 lines (48 loc) · 913 Bytes
/
Matrix.java
File metadata and controls
52 lines (48 loc) · 913 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
import java.util.Scanner;
public class Matrix {
public static void main(String args[])
{
System.out.println("Enter the value of n:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int[][] a=new int[n][n];
int b=1,c=0,d=n-1,e=n-1,f=0,g;
/* b= value
* c=Minnimum value of columns
* d=Maximum value of column
* e=Maximum value of row
* f=Minimum value of row*/
for(g=0;g<=n;g++)
{
for (int i=c;i<=d;i++)
{
a[f][i]=b;
b++;
}
for (int i=f+1;i<=e;i++)
{
a[i][d]=b;
b++;
}
for (int i=d-1;i>=c;i--)
{
a[e][i]=b;
b++;
}
for (int i=e-1;i>=f+1;i--)
{
a[i][c]=b;
b++;
}
c++;f++;d--;e--;
}
for (int i=0;i<a.length;i++)
{
for (int j=0;j<a.length;j++)
{
System.out.print(a[i][j]+ "\t");
}
System.out.println();
}
}
}