Downward triangle star pattern means we want to print a triangle that is downward facing means base is upwards and by default, orientation is leftwards so the desired triangle to be printed should look like
* * * *
* * *
* *
*
Example
Java
public class GFG {
public static void main(String[] args)
{
int rows = 9 ;
for ( int a = rows - 1 ; a >= 0 ; a--) {
for ( int b = 0 ; b <= a; b++) {
System.out.print( "*"
+ " " );
}
System.out.println();
}
}
}
|
Output
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Using Recursion
Java
import java.io.*;
class GFG
{
public static void printRow( int n)
{
if (n == 0 )
{
return ;
}
System.out.print( "* " );
printRow(n - 1 );
}
public static void nextRow( int n)
{
if (n == 0 )
{
return ;
}
printRow(n);
System.out.print( "\n" );
nextRow(n - 1 );
}
public static void main (String[] args)
{
GFG.nextRow( 5 );
}
}
|
Output
* * * * *
* * * *
* * *
* *
*
Time Complexity: O(n2), where n represents the given number of rows.
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 :
29 Dec, 2022
Like Article
Save Article