Latest :

Hollow Inverted Right Triangle Star Pattern Java Program

Java program to print hollow inverted right triangle star pattern program. We have written below the print/draw hollow inverted 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 scanner class method as  sc.nextInt().

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

3) The 1st for loop prints character if the condition if(i==1 || i==n) is true and j<=i is true, with the structure

for(int j=1;j<=i;j++).

After completion of all iterations, the following pattern will be printed.

Using For Loop:

Output:

Using While Loop

1) Checks the condition at while i>0, if true it executes the inner statements.

2) checks the if condition if(i==1 || i==n)

a) if true j initialized to 1, executes the 1st inner while loop, this loop prints character if the condition while(j <=i) is true, then increases the j value by 1, repeats until the condition is false.

b) if false j initialized to 1, while loop prints character if ( if(j==1 || j==i) true and (j<=i)) otherwise prints space, then j value increased by 1, repeats until j<=i is false.

3) Cursor comes to next line i value decreased by 1 and condition at outer while will be checked, repeats until i>0 is false at outer while loop.

Output:

Using Do While Loop

1) In outer do loop checks the if condition

a)if true 1st inner do loop will execute, j initialized to 1 and prints the character once then checks the condition at while(++j <=i), if it is true then prints the character and then checks the condition, repeats until while(++j <=i) condition is false.

b) if false, j initialized to 1, the do loop prints the character if the condition if(j==1 || j==i) is true else prints space, then checks the condition at  while(++j<=i), repeats until while(++j<=i) condition is false.

2) Cursor comes to next line, then checks the condition at outer do-while loop while(–i>0), repeat until this condition is false.

Output:

More Java Programs:

x

Check Also

Java Inverted Mirrored Right Triangle Star Pattern

Java program to print Inverted mirrored right triangle star pattern program. We have written below ...