Beginners Programs :

Hollow Right Triangle Star Pattern Java Program

Java program to print Hollow right triangle star pattern program – We have written below the print/draw Hollow 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 such that you can execute the below codes.

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

Using For Loop

1) Read the entered value using the method nextInt() of scanner class, store it in the variable n.

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

3) The outer for loop iterates from i=1 to n, if “if” condition

a) true, 1st inner loop prints character until j<=i

b) false, 2nd inner loop prints character for j=1 or j=i until j<=i, for j!=1 and j!=n prints space.

Output:

Using While Loop

1) Checks the condition at outer while for the given “i” value, if it is true.

2) Then checks the “if” condition

a) true, j initialized to 1, checks the inner while condition j<=i, if true prints character and increases the j value, repeats until condition j<=i is false.

b) false, j initialized to 1, checks the while condition, condition true then prints characters for  j=1 or j=i otherwise prints space for j!=i and j!=1.

3) Repeats until the condition at outer while is false.

Output:

Using Do-While Loop

1) Checks the “if condition”

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

b) false, 2nd inner loop prints character for j=1 or j=i, prints space other than j=1, j=i values. Then checks the condition while(++j<=i);, repeats until the condition is false.

2) Cursor comes to next line and i value increased by 1, then outer do loop start the execution, repeats until while(i<=n); is false.

Output:

Leave a Reply

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

*