In this article, we are going to learn how to print mirror lower star triangle patterns in Java.
Illustration:
Input: number = 7
Output:
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Methods: We can print mirror lower star triangle pattern using loops, so
- Using for loop
- Using while loop
- Using do-while loop
Method 1: Using for Loop
Example
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int number = 7 ;
int m, n;
for (m = 1 ; m <= number; m++) {
for (n = 1 ; n < m; n++) {
System.out.print( " " );
}
for (n = m; n <= number; n++) {
System.out.print( "*"
+ " " );
}
System.out.println();
}
for (m = number - 1 ; m >= 0 ; m--) {
for (n = 0 ; n < m; n++) {
System.out.print( " " );
}
for (n = m; n <= number - 1 ; n++) {
System.out.print( "*"
+ " " );
}
System.out.println();
}
}
}
|
Output
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Time complexity: O(n^2) where n is given input number
Auxiliary Space: O(1) because it using constant variable
Method 2: Using While Loop
Example
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int number = 7 ;
int m = number;
int n;
while (m > 0 ) {
n = 0 ;
while (n++ < number - m) {
System.out.print( " " );
}
n = 0 ;
while (n++ < (m * 2 ) - 1 ) {
System.out.print( "*" );
}
System.out.println();
m--;
}
m = 1 ;
while (m <= number) {
n = 0 ;
while (n++ < number - m) {
System.out.print( " " );
}
n = 0 ;
while (n++ < (m * 2 ) - 1 ) {
System.out.print( "*" );
}
System.out.println();
m++;
}
}
}
|
Output
*************
***********
*********
*******
*****
***
*
*
***
*****
*******
*********
***********
*************
Output:
Method 3: Using do-while Loop
Implementation:
Example
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int number = 7 ;
int m = number;
int n;
do {
n = 0 ;
do {
System.out.print( " " );
} while (n++ < number - m);
n = 0 ;
do {
System.out.print( "*" );
}
while (n++ < m * 2 - 2 );
System.out.println( "" );
}
while (--m > 0 );
m = 1 ;
do {
n = 0 ;
do {
System.out.print( " " );
} while (n++ < (number - m));
n = 0 ;
do {
System.out.print( "*" );
}
while (n++ < (m * 2 ) - 2 );
System.out.println( "" );
}
while (++m <= number);
}
}
|
Output
*************
***********
*********
*******
*****
***
*
*
***
*****
*******
*********
***********
*************
Time complexity: O(N2) where N is given input size of triangle
Auxiliary Space : O(1) because it using constant variable
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
12 Sep, 2022
Like Article
Save Article