Beginners Programs :

Hollow Mirrored Right Triangle Star Pattern

Java program to print Hollow mirrored right triangle star pattern program. We have written below the print/draw hollow mirrored right triangle 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) Scanner class method nextInt() reads the entered value, initialized to the variable n.

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

3) Checks the condition at outer loop,if true then a) 1st inner for loop runs with the structure for(int j=1;j<=n-i;j++) and prints space if the condition is true.

4) If if condition true

a) 1st inner for loop under if condition, iterates with the structure

for(int j=1;j<=i;j++) and prints character if j<=i is true.

b) If “if” condition is false then 2nd inner loop iterates with the structure

for(int j=1;j<=i;j++) and prints character for j=1 or j=i, prints space for j!=1 and j!=i.

5) Cursor comes to next line and outer loop executes the code, repeats until i<=n is false.

6) In this example n value is 5, for i=1 condition at outer loop i<=n  is true, condition at inner loop j<=n-i is true,so from j=1 to j=4 at 1st row 4 spaces will bee printed. “If” condition is true for i=1, so inner loop prints one character at 1st row, then cursor comes to next line outer loop iterate with i=2, condition at outer loop is true, condition at inner loop is true, so inner loop prints 3 spaces from j=1 to j=3 at 2nd row.”if” condition is false so comes to else part, the for loop prints 2 characters for j=1 and j=2. After completion of all iterations, the following pattern will be printed.

Output:

Using While Loop

1) i value initialized to 1, checks the condition at while i<=n, condition true then j initialized to 1.

2) The 1st inner loop prints the space until  j<=n-i is false.

3) If “if” condition is true

a) 1st inner loop then j=1 and while loop prints character until the condition j<=i is false.

b) condition is false, then while loop prints character until j<=i if the condition if(j==1 || j==i) is true, otherwise it prints space.

Output:

3. Using Do-While Loop

1) j=1, inner do loop prints one space then checks the condition while(++j<=(n-i+1)), if it is true then prints space and checks the condition repeats until condition is false.

2) If “if condition”

a) true, inner do loop prints one character then checks the condition while(++j <=i); repeats until condition is false.

b) false, comes to else part and checks the if condition, it is true for j=1, so prints one character, then checks the while condition (++j<=i), this loop prints character for j=1 or j=i, it prints space other than these j values.

Output:

More Programs:

Leave a Reply

Your email address will not be published. Required fields are marked *

*