You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Print centered pyramid star pattern using nested loops
🧠 Logic:
- Accept user input `n` for the number of pyramid levels.
- Outer loop (`i`) runs from 1 to `n` for each row.
- First inner loop prints `n - i` spaces to center the stars.
- Second inner loop prints `2 * i - 1` stars to form the pyramid shape.
- A newline is printed after each row to maintain pyramid structure.
Signed-off-by: Somesh diwan <[email protected]>
Copy file name to clipboardExpand all lines: Section8LoopAB/Loop 2.O/src/NestedLoopCC4PyramidPattern.java
+15-11Lines changed: 15 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -3,17 +3,15 @@
3
3
publicclassNestedLoopCC4PyramidPattern {
4
4
publicstaticvoidmain(String[] args) {
5
5
Scannerscanner = newScanner(System.in);
6
+
System.out.println( "Enter a Number: ");
6
7
intn =scanner.nextInt();
7
8
8
-
for (inti = 1; i <= n; i++)
9
-
{
9
+
for (inti = 1; i <= n; i++) {
10
10
for (intj = 1; j <= n-i; j++)
11
11
System.out.print(" ");
12
12
13
13
for (intk = 1; k <= 2 * i -1; k++)
14
14
System.out.print("*");
15
-
16
-
17
15
System.out.println();
18
16
}
19
17
}
@@ -23,15 +21,15 @@ public static void main(String[] args) {
23
21
To determine the number of spaces and stars in each row, follow these steps:
24
22
25
23
Spaces:
26
-
27
24
For each row i, the number of spaces decreases as i increases.
28
-
The number of spaces is N - i. For example, when N = 5 and i = 1, spaces = 4; when i = 2, spaces = 3, and so on.
25
+
The number of spaces is N - i.
26
+
For example, when N = 5 and i = 1, spaces = 4; when i = 2, spaces = 3, and so on.
29
27
30
28
Stars:
31
29
32
30
The number of stars in each row increases as i increases.
33
-
34
-
The number of stars is 2 * i - 1. For example, when i = 1, stars = 1; when i = 2, stars = 3, and so on.
31
+
The number of stars is 2 * i - 1.
32
+
For example, when i = 1, stars = 1; when i = 2, stars = 3, and so on.
35
33
36
34
Example Breakdown for N = 5:
37
35
Row 1: Spaces = 4, Stars = 1 → *
@@ -42,10 +40,14 @@ public static void main(String[] args) {
42
40
*/
43
41
44
42
/*
45
-
The solution works for both even and odd values of N. The logic for spaces and stars remains the same regardless of whether N is even or odd. Let me explain:
43
+
The solution works for both even and odd values of N.
44
+
The logic for spaces and stars remains the same regardless of whether N is even or odd.
45
+
46
+
Let me explain:
46
47
47
48
For Odd N:
48
49
The pyramid will have a single star at the top and expand symmetrically downward.
50
+
49
51
Example for N = 5 (odd):
50
52
*
51
53
***
@@ -68,7 +70,8 @@ public static void main(String[] args) {
68
70
*/
69
71
70
72
71
-
/*Outer Loop: Iterate over the rows (from 1 to N).
73
+
/*
74
+
Outer Loop: Iterate over the rows (from 1 to N).
72
75
First Inner Loop: Print the spaces for alignment.
73
76
Second Inner Loop: Print the stars for the pyramid.
74
77
New Line: Move to the next line after each row
@@ -89,6 +92,7 @@ The number of stars increases as i increases (2 * i - 1).
89
92
New Line:
90
93
91
94
After printing spaces and stars for a row, move to the next line using System.out.println().
95
+
92
96
Example for N = 3:
93
97
Row 1:
94
98
Spaces: 3 - 1 = 2 →
@@ -104,4 +108,4 @@ The number of stars increases as i increases (2 * i - 1).
0 commit comments