-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava Program to Print Diamond Star Pattern
More file actions
108 lines (79 loc) · 1.99 KB
/
Copy pathJava Program to Print Diamond Star Pattern
File metadata and controls
108 lines (79 loc) · 1.99 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Java program to Print Diamond Star Pattern
// Using do-while loop
// Importing input output classes
import java.io.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing variables
// Variable initialized to the row where max star
// should be there as after that they decreases to
// give diamond pattern
int number = 7;
// Diamond starting with single star in first row,so
// initialized
int m = 1;
// Columnar printing
int n;
// Outer loop 1
// Prints the first half diamond
do {
n = 1;
// Inner loop 1
// Prints space until ++n <= number - m + 1 is
// false
do {
// Print whitespace between
System.out.print(" ");
}
// Condition for inside do-while loop 1
while (++n <= number - m + 1);
// Now
n = 1;
// Inner loop 2
// Prints star until ++n <= m * 2 - 1 is false
do {
// Print star
System.out.print("*");
}
// Condition for inner do-while loop 2
while (++n <= m * 2 - 1);
// A new row requires a new line
System.out.println();
}
// Condition for outer do-while loop 1
while (++m <= number);
// Now we are done with printing the upper half
// diamond.
// Note: Not to print the bottom row again in lower
// half diamond printing Hence variable t be
// initialized should one lesser than number
m = number - 1;
// Outer loop 2
// Prints the second half diamond
do {
n = 1;
// Inner loop 1
// Prints space until ++n <= number - m + 1 is
// false
do {
// Print whitespace between
System.out.print(" ");
} while (++n <= number - m + 1);
n = 1;
// Inner loop 2
// Prints star until ++n <= m * 2 - 1 is false
do {
// Prints star
System.out.print("*");
} while (++n <= m * 2 - 1);
// By now done with one row of lower diamond
// printing so a new line is required
System.out.println();
}
// Condition for outer do-while loop 2
while (--m > 0);
}
}