The Mirror Lower Star Triangle Pattern is a pattern-printing program in Java where an inverted right triangle is followed by its mirror image, creating a vertically symmetric pattern. It is an excellent exercise for understanding nested loops, spacing, and pattern symmetry.
- Leading spaces increase in the first half and decrease in the second half.
- Can be implemented using for, while, and do-while loops.
Illustration
Input: Rows = 5
Output:
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Method 1: Using For Loop
Approach
- Initialize the number of rows.
- Print the upper inverted triangle using nested
forloops. - Print the lower mirrored triangle using another set of nested loops.
- Display spaces before stars to create the mirror effect.
import java.io.*;
// Main class
class GFG {
// Method
// Main driver method
public static void main(String[] args)
{
// Declare and initialize variable to
// Size of the triangle
int number = 7;
// Declaring two variables for rows and columns
int m, n;
// Outer loop 1
// Prints the first half triangle
for (m = 1; m <= number; m++) {
// Inner loop 1
for (n = 1; n < m; n++) {
// Print whitespace
System.out.print(" ");
}
// Inner loop 2
for (n = m; n <= number; n++) {
// Print star
System.out.print("*"
+ " ");
}
// Ending line after each row
System.out.println();
}
// Outer loop 2
// prints the second half triangle
for (m = number - 1; m >= 0; m--) {
// Inner loop 1
for (n = 0; n < m; n++) {
// Print whitespace
System.out.print(" ");
}
// Inner loop 2
for (n = m; n <= number - 1; n++) {
// Print star
System.out.print("*"
+ " ");
}
// Ending line after each row
System.out.println();
}
}
}
Output
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Explanation: The first set of nested loops prints an inverted triangle by increasing spaces and decreasing stars. The second set prints the same pattern in reverse order, producing a vertically mirrored lower star triangle.
Method 2: Using While Loop
Approach
- Use a
whileloop to print the upper half. - Increase spaces and decrease stars after each row.
- Reset the counter.
- Use another
whileloop to print the mirrored lower half.
import java.io.*;
// Main Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declare and initialize variable to
// Size of the triangle
int number = 7;
int m = number;
int n;
// Outer loop 1
// prints the first half triangle
// Till condition holds true
while (m > 0) {
n = 0;
// Inner loop 1
// prints space until n++ < number - m is false
while (n++ < number - m) {
// print whitespace
System.out.print(" ");
}
n = 0;
// Inner loop 2
// Prints star until n++ < (m * 2) - 1 is false
while (n++ < (m * 2) - 1) {
// Print star
System.out.print("*");
}
// Ending line after each row
System.out.println();
// Decrementing by one
m--;
}
m = 1;
// Outer loop 2
// prints the second half triangle
while (m <= number) {
n = 0;
// Inner loop 1
// prints space until n++ < number - m is false
while (n++ < number - m) {
// Print whitespace
System.out.print(" ");
}
n = 0;
// Inner loop 2
// Prints star until n++ < (m * 2) - 1 is false
while (n++ < (m * 2) - 1) {
// print star
System.out.print("*");
}
// Ending line after each row
System.out.println();
m++;
}
}
}
Output
*************
***********
*********
*******
*****
***
*
*
***
*****
*******
*********
***********
*************Explanation: The first while loop prints the inverted triangle, while the second while loop prints the mirrored triangle. Spaces and stars are adjusted in every iteration to maintain symmetry.
Method 3: Using Do-While Loop
Approach
- Print the first half using nested
do-whileloops. - Reset the row counter.
- Print the mirrored lower half using another set of
do-whileloops. - Continue until all rows are printed.
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declare variable with the
// Size of the triangle
int number = 7;
int m = number;
int n;
// Outer loop 1
// prints the first half triangle
do {
n = 0;
// Inner loop 1
// prints space until n++ < number - m is false
do {
// Print whitespaces
System.out.print(" ");
} while (n++ < number - m);
n = 0;
// Inner loop 2
// prints star until n++ < m * 2 - 2 is false
do {
// Print star
System.out.print("*");
}
while (n++ < m * 2 - 2);
System.out.println("");
}
// Condition check for do-while
while (--m > 0);
m = 1;
// Outer loop 2
// prints the second half triangle
do {
n = 0;
// Inner loop 1
// prints space until n++ < (number - m) is
// false
do {
// Print whitespace
System.out.print(" ");
} while (n++ < (number - m));
n = 0;
// Inner loop 2
// prints star until n++ < (m * 2) - 2 is false
do {
// Print star
System.out.print("*");
}
while (n++ < (m * 2) - 2);
System.out.println("");
}
// Condition check for do-while
while (++m <= number);
}
}
Output
*************
***********
*********
*******
*****
***
*
*
***
*****
*******
*********
***********
*************Explanation: The do-while loops ensure that each row is printed at least once. The first half generates the inverted triangle, while the second half prints its mirror image, producing the complete mirror lower star triangle.