Skip to content

Commit 2254c6d

Browse files
committed
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]>
1 parent 14ce147 commit 2254c6d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

Section8LoopAB/Loop 2.O/src/NestedLoopCC4PyramidPattern.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
public class NestedLoopCC4PyramidPattern {
44
public static void main(String[] args) {
55
Scanner scanner = new Scanner(System.in);
6+
System.out.println( "Enter a Number: ");
67
int n =scanner.nextInt();
78

8-
for (int i = 1; i <= n; i++)
9-
{
9+
for (int i = 1; i <= n; i++) {
1010
for (int j = 1; j <= n-i; j++)
1111
System.out.print(" ");
1212

1313
for (int k = 1; k <= 2 * i -1; k++)
1414
System.out.print("*");
15-
16-
1715
System.out.println();
1816
}
1917
}
@@ -23,15 +21,15 @@ public static void main(String[] args) {
2321
To determine the number of spaces and stars in each row, follow these steps:
2422
2523
Spaces:
26-
2724
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.
2927
3028
Stars:
3129
3230
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.
3533
3634
Example Breakdown for N = 5:
3735
Row 1: Spaces = 4, Stars = 1 → *
@@ -42,10 +40,14 @@ public static void main(String[] args) {
4240
*/
4341

4442
/*
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:
4647
4748
For Odd N:
4849
The pyramid will have a single star at the top and expand symmetrically downward.
50+
4951
Example for N = 5 (odd):
5052
*
5153
***
@@ -68,7 +70,8 @@ public static void main(String[] args) {
6870
*/
6971

7072

71-
/*Outer Loop: Iterate over the rows (from 1 to N).
73+
/*
74+
Outer Loop: Iterate over the rows (from 1 to N).
7275
First Inner Loop: Print the spaces for alignment.
7376
Second Inner Loop: Print the stars for the pyramid.
7477
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).
8992
New Line:
9093
9194
After printing spaces and stars for a row, move to the next line using System.out.println().
95+
9296
Example for N = 3:
9397
Row 1:
9498
Spaces: 3 - 1 = 2 →
@@ -104,4 +108,4 @@ The number of stars increases as i increases (2 * i - 1).
104108
Spaces: 3 - 3 = 0 → ``
105109
Stars: 2 * 3 - 1 = 5 → *****
106110
Output: *****
107-
*/
111+
*/

0 commit comments

Comments
 (0)