-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumericDiamondPattern.java
51 lines (42 loc) · 1.19 KB
/
NumericDiamondPattern.java
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
package Pattern.NumberPattern;
public class NumericDiamondPattern {
public static void main(String[] args) {
int n = 4, count = 0;
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
System.out.print(" ");
}
count+=i;
for (int j = 1; j <= i; j++) {
System.out.print(count+" ");
if(j<i) count--; else count+=i-1;
}
System.out.println();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int j = i; j <= n; j++) {
System.out.print(count+" ");
count--;
}
System.out.println();
}
}
}
// Input:
// n = 4
// Output:
// 1
// 3 2
// 6 5 4
// 10 9 8 7
// 10 9 8 7
// 6 5 4
// 3 2
// 1
// Explaination:
// First upperTriangle like reverse number printing
// Add the row value to count value minus each columns when last column is reached then
// add current count with row value - 1 (to reach the initial value)