-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
hexagon.java
65 lines (56 loc) · 1.52 KB
/
hexagon.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class hexagon
{
static void printHexagon(int n)
{
int l = 2 * n - 1;
/**** Print upper part *****/
for (int i = 0; i < n; i++)
{
int elem = i + n;
// loop for printing the
for (int k = 0; k < elem; k++)
{
if ((k == n + i - 1) ||
(k == n - i - 1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.print("\n");
}
/**** Print middle part *****/
for (int m = 0; m < n - 2; m++)
{
for (int j = 0; j < l; j++)
{
if (j == 0 || j == l - 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.print("\n");
}
/**** Print Lower part *****/
int r = n - 1;
for (int h = r; h >= 0; h--)
{
int elem = h + n;
for (int k = 0; k < elem; k++)
{
if ((k == n + h - 1) ||
(k == n - h - 1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.print("\n");
}
}
public static void main (String[] args)
{
// length of a side
// n >= 2
int n = 3;
printHexagon(n);
}
}