-
Notifications
You must be signed in to change notification settings - Fork 6
/
hollowDiamond.java
70 lines (61 loc) · 1.51 KB
/
hollowDiamond.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
66
67
68
69
/* Code for printing Hollow Diamond pattern
For n = 4
Output:
********
*** ***
** **
* *
* *
** **
*** ***
********
Solution:
1. First we divide the pattern horizontally into 2 halves.
2. The 1st for loop prints all the pattern in the 1st half
3. The 2nd for loop prints all the pattern in the 2nd half
*/
public import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
hollowDiamond(4); // For n = 4
}
public static void hollowDiamond(int n){
// Print i number of stars
// 1st for loop
for (int i=1; i<=n; i++)
{
for (int j = i; j <= n; j++)
{
System.out.print("*");
}
for (int k = 1; k <= i*2-2; k++) // this loop is used to provide spaces
{
System.out.print(" ");
}
for (int l = i; l <= n; l++)
{
System.out.print("*");
}
System.out.println();
}
// 2nd for loop
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
for (int k = i*2-2; k < n*2-2; k++) // this loop is used to provide spaces
{
System.out.print(" ");
}
for (int l = 1; l <= i; l++)
{
System.out.print("*");
}
System.out.println();
}
}
}