-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiamondStarPattern.java
More file actions
47 lines (41 loc) · 1.4 KB
/
DiamondStarPattern.java
File metadata and controls
47 lines (41 loc) · 1.4 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
import java.util.Scanner;
public class DiamondStarPattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows (odd number): ");
int rows = scanner.nextInt();
if (rows % 2 == 0) {
System.out.println("Please enter an odd number of rows.");
} else {
int spaces = rows / 2;
int stars = 1;
// Upper half of the diamond
for (int i = 0; i < rows / 2 + 1; i++) {
for (int j = 0; j < spaces; j++) {
System.out.print(" ");
}
for (int j = 0; j < stars; j++) {
System.out.print("*");
}
System.out.println();
spaces--;
stars += 2;
}
// Lower half of the diamond
spaces = 1;
stars = rows - 2;
for (int i = 0; i < rows / 2; i++) {
for (int j = 0; j < spaces; j++) {
System.out.print(" ");
}
for (int j = 0; j < stars; j++) {
System.out.print("*");
}
System.out.println();
spaces++;
stars -= 2;
}
}
scanner.close();
}
}