Java Program To Print Mirrored Rhombus Star Pattern | Programs |
Beginners Programs :

Java Program To Print Mirrored Rhombus Star Pattern | Programs

Java program to print mirrored rhombus star pattern program. We have written the below print/draw mirrored rhombus asterisk/star pattern program in four different ways with sample example and output, check it out. At the end of the program, we added compiler such that you can execute the below codes.

  • Print Mirrored Rhombus Star Pattern – Using For Loop
  • Print – Using While Loop
  • print – Using Do While Loop

1. Using For Loop

 

  1. Read the n value using scanner class, store it in the variable n.

2) Run the outer for loop with the structure for(int i=1;i<=n;i++) to iterate through rows

3) The 1st inner loop prints space from j=n-i to j<n, 2nd inner loop prints charter from j=1 to j=n.

In this example n=6, for i=1, 1st inner loop prints one space for j=5. For j=6 condition fails then comes to next fro loop. 2nd loop prints charters from j=1 to j=6 at 1st row. next n=2 ,1st loop prints spaces for j=4,5. 2nd loop prints charters from j=1 to 6 at 2nd row.after  2 iterations this pattern will be printed.

After all iterations, the given pattern will be printed.

Output:

 

2. Using While Loop

  1. While loop is entry checking loop, it checks the condition then only executes the code.

2) Checks the condition at while, while(i<=n),if it is true then j initialized to 1 and 1st inner loop prints space until the condition  while(–j>0) is false

3) j initialized to 1,2nd inner loop prints charter until the condition  while(j++<=n) is false.

4) Next cursor comes to next line and i value increased by 1,  and again starts the execution of outer while loop until  (i<=n) the condition is false.

Output:

 

3. Using Do-While Loop

  1. The do-while loop is exit checking loop, it executes the code one time then it checks the condition.

2) For i,j values 1st inner do loop prints one space then checks the code while(–j>0), if this code is true then it prints the space until the condition is false.

3) For j=1, 2nd inner loop prints one charter then checks the condition while(++j<=n), if this condition true it prints charter until the condition is false.

4) Cursor comes to next line and i value increased by 1, then checks the condition at outer do-while loop (i<=n), if this condition is true then the outer do-while loop start the execution again, repeats until the condition i<=n is false.

Output: