Latest :

Hollow Rhombus Star Pattern Java Program

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

  • Using For Loop
  • Using While Loop
  • Using Do While Loop

 Using For Loop

1) Read entered value store it in the variable n.

2) The outer for loop iterate through rows with the structure for(int i=1;i<=n;i++) and run the inner loops to iterate through columns.

3) The outer for loop iterates from i=1 to i=n, the 1st inner for loop iterates with the structure for(int j=1;j<=n-i;j++) and prints space if j<=n-i is true.

4) 2nd inner for loop prints characters from j=1 to j=n, if i==1 or i==n

if i!=1 || i!=n then 3rd inner loop prints characters if j=1 or j=n otherwise prints space.

next cursor comes to next line and i value increased, i=2 the condition at outer loop is true if the condition is false for i=2 so comes to else part, the for loop prints character for j=1 and j=5. Prints spaces for j=2, 3, 4.

  • After all iterations, the complete pattern will be printed.

Output:

Using While Loop

1) Check the condition at outer while for the given i value, if this condition is true then j=1 .

2) The 1st inner while loop prints spaces until while(j++<=n-i) is false.

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

a) true then 2nd inner loop prints character until while(j <=n) the condition is false.

b) false the 3rd inner loop prints character until while(j<=n) is false if j=1 or j=n, otherwise it prints space for j!=1, n.

Output:

Using Do-While Loop

1) The outer do loop starts the execution j=1, the 1st inner do loop prints one space then checks the condition while(j++<=n-i), if this condition is true it prints character until the condition is false.

a) true then inner do loop prints one character then checks the condition while(++j <=n), if this condition is true then it prints the character until the condition is true.

b) false the do loop prints character if j=1 or j=n is true otherwise prints space and then checks the condition while(++j<=n). If it is true then it executes the code, repeats until while(++j<=n) is false.

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

Output:

x

Check Also

Rhombus Star Pattern Program In Java – Patterns

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