Beginners Programs :

Java Mirrored Right Triangle Star Pattern Programs | Patterns

Java program to print mirrored right triangle star pattern program. We have written below the print/draw 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.

  • Print Mirrored Right Triangle Star Pattern – Using For Loop
  • Print – Using While Loop
  • print – Using Do While Loop

Using For Loop

1) Read the n value.

2) Run the outer for loop with the structure for(int i=1;i<=n;i++) to iterate through rows from i=1 to i=n.

3) The condition of outer loop is true then 1st inner loop runs with the structure for(int j=0;j<n-i;j++) and

prints space if i<=n.

4) 2nd inner loop runs with the structure and prints character if j<i.

5) Then cursor comes to next line and 2nd iteration will begin, repeat until i<=n.

In this example n=5, for i=1 i<=n is true, so in 1st inner loop for j=0 j<n-i is true so it prints space,for j=1,2,3 condition true so it prints spaces, in 2nd  inner loop for j=0,j<i is true so it prints character,for j=1 condition fails. After 1st iteration —-* will be printed(4 spaces and one character), after all the iterations, this pattern will be printed

Output:

Using While Loop

1) Checks the condition at outer while loop i<=n,if true j initialized to 0.

2) 1st inner while loop prints space if  j++<(n-i) is true, repeats until condition fails.

3) j value initialized to 0, 2nd inner while loop prints character j<i is true, and j increased by 1, repeat until the condition is false.

4) Cursor comes to next line, i value increased by 1 and again checks the outer while condition, repeat until the condition is false at outer loop.

Output:

Using Do-While Loop

1) j initialized to 0. inner do loop prints space once then it checks the condition (++j<n-i) if it is true then it prints space, repeats until the condition is false.

2) Next j initialized to 0, 2nd inner loop prints the character then checks the condition while(j++<i), if true it prints character again checks the condition repeats until the condition fails.

3) Cursor comes to next line, then checks the condition at outer do-while loop while(++i<n) if it is true then outer do loop executes the code, repeats until the condition at outer do while loop is false.

Output:

More Java Programs: