Beginners Programs :

Java Hollow Inverted Mirrored Right Triangle Star Pattern

Java program to print hollow inverted mirrored right triangle star pattern program. We have written below the print/draw hollow inverted 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) Read the n value using sc.nextIne(), and store that value in the variable n.

2) To iterate through rows run the outer for loop with the structure for(int i=n;i>0;i–), to iterate through columns run the inner loops.

3) The outer loop iterates with the structure for(int i=n;i>0;i–), if i>0 true then it executes the inner loop.

4) The 1st inner loop runs with the structure for(int j=n-i;j>0;j–) and prints space if j>0.

5) If “if” condition if(i==1 || i==n) is

a) true, i.e. for  i=1 or i=n, the for loop runs with the structure for(int j=1;j<=i;j++) and prints character  if j<=1 is true. b) false i.e for i!=1 and 1!=n the for loop prints the character if if(j==1 || j==i) is true, else prints space, this for loop runs with the structure for(int j=1;j<=i;j++). Then cursor comes to next line and the outer loop will run. This will repeat until i>0 at outer loop.

6) After completion of 1st iteration the will be print like this. **********

ex. n=10 so condition at outer loop i>0 is true so come to inner loop and checks the condition at for, “j>o” fails for i=n=10 so j=n-i=0, next comes to if condition, here i==n is true so for j=1 to 10 characters will be printed(this is 1st iteration of outer loop).

Output:

Using While Loop

1) Checks the condition at outer loop i>0, if true it comes to inner loop, the inner loop prints space if j–>0 is true, then decreases the j value, again checks the condition at while, repeats until j–>0. Otherwise, checks the if condition.

a) if true, the inner loop prints character if the condition j<=i is true and increases the j value by 1 and again checks the condition at while, it repeats until j<=1 is false.

b) if false, the 2nd inner loop checks the condition at while while(j<=i) if it is true then checks the “if” condition present in this loop, if condition true – prints character otherwise prints space, now j value will be increased by 1, again checks the condition at while, repeats until j<=i is false.

2) cursor comes to next line i value decreased by 1 and checks the condition at outer while, repeats until i>0 is false.

Output:

Using Do-While Loop

1) In do-while loop the 1st while loop prints the space if the condition is true.

2) Checks the if condition a) true it prints one character and checks the condition while(++j <=i) if it is true it will print another character and checks the condition at while, repeats until ++j<=i is false.

b) false, checks the if condition at inner do-while loop if(j==1 || j==i) true prints character otherwise prints space, then checks the condition at while while(++j<=i); if it is true then it repeats the do loop until condition if false.

3) Cursor comes to the next line and the outer do-while loop executes the code until while(–i>0); is false.

Output: