Java Program to Print Upper Star Triangle Pattern
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.
Last Updated :
11 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...