Java Program to Print Downward Triangle Star Pattern
Last Updated :
29 Dec, 2022
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...