Latest :

Java Inverted Mirrored Right Triangle Star Pattern

Java program to print Inverted mirrored right triangle star pattern program. We have written below the print/draw 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 the scanner object sc and store the 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 inner loops.

3) The outer loop executes the inner loops if the condition i>0 is true. The 1st inner loop prints space if the condition j>0, it runs with the structure for(int j=n-i;j>0;j–), the 2nd inner loop prints character if the condition j<i is true,runs with the structure  for(int j=0;j<i;j++).

4) In this example n=7, the condition i>0 is true at the outer loop, in the 1st inner loop the condition fails, comes to next inner loop checks the condition. Here if the condition is true then it prints the character, this loop prints the characters from j=0 to j=6, 1st iteration completed, this pattern will be printed *******.

5) for n=6, the 1st inner loop prints one space, the 2nd inner loop prints character from j=0 to j=5. After total iterations of the outer loop, this pattern will be printed.

Output:

Using While Loop

1) Checks the condition at while i>0 if it is true then j value initialized to j=n-i

a) 1st inner loop prints space if j–>0 is true, this loop repeats until the condition is false. If false, j initialized to 0.

b) 2nd inner loop prints character if j++<i is true, this loop repeats until the condition is false, executes the next statement “System.out.println();

2) Cursor comes to next line i value decreased by value 1 and checks the condition at outer loop. It repeats until the condition is false

Output:

Using Do-While Loop

1) j value initialized to n-i and while loop prints the space if the condition j–>0 is true. If false j initialized to j=0.

2) Inner do loop prints the character once then it checks the while condition ++j<i, if it is true then it prints character, it repeats until the condition while(++j<i); is false, next cursor comes to next line.

3) Then it checks the condition while(–i>0) at outer do loop, if it is true then do loop executes the inner code, it repeats until the condition is false.

Output:

For More Java Programs Check Here – More than 500+ Java Programs are Published Here

x

Check Also

Prime Number Java Program – 1 to 100 & 1 to N | Programs

Prime Number Java Program –  Java Program to Check Whether a Number is Prime or ...