Skip to content

Commit 714ce2f

Browse files
authored
Merge pull request #79 from arpitjain22june/patch-11
Create PatternHourGlass.java
2 parents accf9cf + f259d8c commit 714ce2f

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//5
2+
//5 4 3 2 1 0 1 2 3 4 5
3+
// 4 3 2 1 0 1 2 3 4
4+
// 3 2 1 0 1 2 3
5+
// 2 1 0 1 2
6+
// 1 0 1
7+
// 0
8+
// 1 0 1
9+
// 2 1 0 1 2
10+
// 3 2 1 0 1 2 3
11+
// 4 3 2 1 0 1 2 3 4
12+
//5 4 3 2 1 0 1 2 3 4 5
13+
14+
import java.util.Scanner;
15+
16+
public class PatternHourGlass {
17+
18+
public static void main(String[] args) {
19+
Scanner sc = new Scanner(System.in);
20+
int n = 0;
21+
if (sc.hasNext()) {
22+
n = sc.nextInt();
23+
}
24+
int nr=2*n+1;
25+
int nst=nr;
26+
int nsp=0;
27+
int row=1;
28+
int val;
29+
while(row<=nr) {
30+
if(row<=nr/2+1) {
31+
val=n-row+1;
32+
}else {
33+
val=row-nr/2-1;
34+
}
35+
for(int csp = 1; csp<=nsp;csp++) {
36+
System.out.print(" ");
37+
}
38+
for(int cst = 1; cst<=nst;cst++) {
39+
if(cst<nst/2+1) {
40+
System.out.print(val--+" ");
41+
}else if(cst==nst/2+1){
42+
System.out.print(val +" ");
43+
}else {
44+
System.out.print(++val +" ");
45+
}
46+
}
47+
System.out.println();
48+
if(row<=nr/2) {
49+
nsp+=2;
50+
nst-=2;
51+
}else {
52+
nsp-=2;
53+
nst+=2;
54+
}
55+
row++;
56+
57+
}
58+
}
59+
60+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Take N as input. For a value of N=5, we wish to draw the following pattern :
2+
3+
5 4 3 2 1 0 1 2 3 4 5
4+
4 3 2 1 0 1 2 3 4
5+
3 2 1 0 1 2 3
6+
2 1 0 1 2
7+
1 0 1
8+
0
9+
1 0 1
10+
2 1 0 1 2
11+
3 2 1 0 1 2 3
12+
4 3 2 1 0 1 2 3 4
13+
5 4 3 2 1 0 1 2 3 4 5
14+
Input Format
15+
Take N as input.
16+
17+
Constraints
18+
N <= 20
19+
20+
Output Format
21+
Pattern should be printed with a space between every two values.
22+
23+
Sample Input
24+
5
25+

0 commit comments

Comments
 (0)