error code: 523 Java Program To Print Hollow Mirrored Rhombus | 4 Ways
Latest :

Java Program To Print Hollow Mirrored Rhombus | 4 Ways

Java program to print or draw the hollow mirrored rhombus star/asterisk in four different ways. The following program has been written in four different ways along the sample example program and outputs. At this end of this section, we added an execution tool where you can able to execute the program. Learn more about what are data types in Java, 500+ programs for beginners here. Also, do check out our new article about the java interview questions.

Table of contents:

  • Print Mirrored Rhombus Java Program – Using For Loop
  • Print Hollow Mirrored Rhombus – Using While Loop
  • Print – Using Do While Loop

1. Using For Loop

[wp_ad_camp_3]

1) Read the n value using scanner object sc, and store thatvalue in the variable n.

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

3) The 1st-row iterates with the structure for(int j=i;j>0;j–) and prints spaces if j>0.

4) If  if(i==1 || i==n) is

  • a)true, then for loop with the structure for(int j=1;j<=n;j++) and prints charters from j=1 to n.
  • b) false,the loop with the structure for(int j=1;j<=n;j++) and prints charters at j=1 or j=n,prints spaces at if j!=1 and j!=n .

5) Cursor comes to next line and the outer loop starts the execution again, repeats until the condition at outer for loop is false.

In this example n=5, for i=1 the condition i<=n is true, 1st inner loop prints one space for j=i, then comes to if condition, if condition is true for i=1, so the inner loop prints charters from j=1 to j=5, After 1st iteration at 1st row one space and 5 charters will be printed.

Next cursor comes to next line and i value increased by 1.for i=2, the 1st inner loop prints 2 spaces for j=2,1.

Here if the condition fails for i=2 so comes to else part then inner loop prints charters at j=1 and j=5,  and prints spaces for j=2,3,4. after 2 iterations this pattern will be printed.

After all iterations the total pattern will be printed as follows:

Output:

 

2. Hollow Mirrored Rhombus Star Pattern – Using While Loop

1) while loop is entry checking loop, it checks the code then executes the code.

2) Check the Condit, on at while (i<=n) for given value of i, if this condition is true then j initialized to 1, condition while and 1st inner loop print condition while(–j>0) is false.

3) If   if(i==1 || i==n) condition is a) true , j=1, and inner while loop prints charter until the condition  while(j <=n)  is false. b)false, come to else part then  while loop prints charter at j=1 or j=n,until while(j<=n) condition is false, prints space at j!=1 and j!=n .

4) Cursor comes to next line, i value increased by 1, and outer while loop executes the code again, repeats until i<=n condition is false.

Output:
 

3. Hollow Mirrored Rhombus Star Pattern -Do-while

1) The do-while loop is exit checking loop, it executes the code onetime then checks the code.

2) For given i,j do loop starts the execution, the inner do loop prints the one space then checks the condition while (–j>0),  if it is true then it prints space until the condition while(–j>0) is false.

3) Checks the if condition

  • a)true inner do loop prints one charter then checks the condition  while(++j<=n), it prints until the condition while(++j<=n) is false.
  • b) false, comes to else part and for j=1 the inner loop prints one charter then checks the condition while (++j<=n),it is false so prints space, repeats until the condition ++j<=n is false.

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

Output:
More Programs for you:

x

Check Also

X Star Pattern Java Program – Patterns

Java program to print X star pattern program – We have written the below print/draw ...