-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ8.java
More file actions
37 lines (35 loc) · 1.24 KB
/
Copy pathJ8.java
File metadata and controls
37 lines (35 loc) · 1.24 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
/*
> Date Created: 24/04/2026
> Author: Ishaan Rastogi
> Purpose: To print a solid diamond pattern (pyramid + inverse pyramid)
> Operating System: This is only for Windows OS, it may or may not work on other OS
> Program Status: 100% Working
*/
import java.util.*;
class J8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows in the pyramid: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("* ");
}
System.out.println();
}
// Inverted pyramid has 1 row less than the above rows => n - 1
for (int i = 1; i <= n - 1; i++) { // n - 1 = n - 1
for (int j = 1; j <= i; j++) { // + 1
System.out.print(" ");
}
// we are -1 twice because for start and end of the loop
for (int k = 1; k <= 2 * (n - i) - 1; k++) { // 2 * (n - i) + 1 - 1 - 1 = 2 * (n - i) - 1
System.out.print("* ");
}
System.out.println();
}
}
}