Skip to content

Commit 92a41df

Browse files
committed
feat: Generate diamond pattern using nested loops with centered alignment
🧠 Logic: - Take user input `N` to define the number of rows in the upper half of the diamond. - First loop builds the top pyramid: - Print `N - i` spaces followed by `2*i - 1` stars for each row `i`. - Second loop builds the inverted bottom pyramid: - Iterate from `N-1` down to `1` for symmetric pattern. - Print `N - i` spaces followed by `2*i - 1` stars. - The combination results in a symmetric diamond pattern. Signed-off-by: Somesh diwan <[email protected]>
1 parent 2254c6d commit 92a41df

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import java.util.Scanner;
22

3-
public class NestedLoopdiamondpattern
4-
{
5-
public static void main(String[] args)
6-
{
3+
public class NestedLoopdiamondpattern {
4+
public static void main(String[] args) {
75
Scanner scanner = new Scanner(System.in);
6+
System.out.println("Enter a Number: ");
7+
88
int N = scanner.nextInt();
99

10-
for(int i = 1; i <= N; i++)
11-
{
10+
for(int i = 1; i <= N; i++) {
1211
for (int j = 1; j <= N - i; j++)
1312
System.out.print(" "); //Spaces printing
1413

1514
for(int k = 1; k<= 2 * i - 1; k++)
1615
System.out.print("*"); //stars
1716
System.out.println();
1817
}
19-
for(int i = N-1; i>=1; i--)
20-
{
18+
for(int i = N-1; i>=1; i--) {
2119
for (int j = 1; j<=N - i; j++)
2220
System.out.print(" ");
2321

@@ -34,11 +32,10 @@ You need to print a diamond shape using stars (*).
3432
The diamond has two parts: the upper part and the lower part.
3533
3634
Upper Part:
37-
3835
The number of stars increases as you go down the rows.
3936
The number of spaces decreases as you go down the rows.
40-
Lower Part:
4137
38+
Lower Part:
4239
The number of stars decreases as you go down the rows.
4340
The number of spaces increases as you go down the rows.
4441
@@ -55,4 +52,4 @@ You need to print a diamond shape using stars (*).
5552
The diamond is centered, so spaces are used to align the stars.
5653
The number of stars in each row follows a pattern: 2 * row - 1.
5754
The number of spaces is calculated as N - row for the upper part and N - row for the lower part.
58-
*/
55+
*/

0 commit comments

Comments
 (0)