-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiamond.java
More file actions
25 lines (21 loc) · 800 Bytes
/
Copy pathDiamond.java
File metadata and controls
25 lines (21 loc) · 800 Bytes
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
import java.util.Scanner;
public class Diamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int n = sc.nextInt();
// Upper half
for (int i = 1; i <= n; i += 2) {
for (int j = 0; j < (n - i) / 2; j++) System.out.print(" "); // Print spaces
for (int j = 0; j < i; j++) System.out.print("*"); // Print stars
System.out.println();
}
// Lower half
for (int i = n - 2; i > 0; i -= 2) {
for (int j = 0; j < (n - i) / 2; j++) System.out.print(" ");
for (int j = 0; j < i; j++) System.out.print("*");
System.out.println();
}
sc.close();
}
}