forked from ravitomar7/Lab_Test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.java
More file actions
47 lines (45 loc) · 814 Bytes
/
pattern.java
File metadata and controls
47 lines (45 loc) · 814 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
package pack1;
import java.util.Scanner;
public class pattern
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of elements for pattern printing");
int n=sc.nextInt();
int ar[][]=new int[n][n];
int k=1, c1=0, c2=n-1, r1=0, r2=n-1;
while(k<=n*n)
{
for(int i=c1;i<=c2;i++)
{
ar[r1][i]=k++;
}
for(int j=r1+1;j<=r2;j++)
{
ar[j][c2]=k++;
}
for(int i=c2-1;i>=c1;i--)
{
ar[r2][i]=k++;
}
for(int j=r2-1;j>=r1+1;j--)
{
ar[j][c1]=k++;
}
c1++;
c2--;
r1++;
r2--;
}
System.out.println("pattern is");
for(int x=0;x<n;x++)
{
for(int y=0;y<n;y++)
{
System.out.print(ar[x][y]+"\t");
}
System.out.println();
}
}
}