The upper star triangle pattern means the base has to be at the bottom and there will only be one star to be printed in the first row. Here is the issue that will arise is what way we traverse be it forward or backward we can’t ignore the white spaces, so we are not able to print a star in the first row using only two nested for loops. The answer to this is very simple as we will break this problem into two halves, we will be running two nested for loops, one managing white spaces and the other managing pattern printing rest remains the same.
Example
Java
public class GFG {
public static void main(String[] args)
{
int k = 9 ;
for ( int a = 0 ; a <= k; a++) {
for ( int b = 1 ; b <= k - a; b++) {
System.out.print( " " );
}
for ( int l = 0 ; l <= a; l++) {
System.out.print( "*" );
}
System.out.println( "" );
}
}
}
|
Output
*
**
***
****
*****
******
*******
********
*********
**********
Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
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 :
11 Oct, 2022
Like Article
Save Article