In this article, we will learn about printing Left Triangle Star Pattern.
Examples:
Input : n = 5
Output:
*
* *
* * *
* * * *
* * * * *
Left Triangle Star Pattern:
Java
import java.io.*;
public class GFG {
public static void StarleftTriangle( int k)
{
int a, b;
for (a = 0 ; a < k; a++) {
for (b = 2 * (k - a); b >= 0 ; b--) {
System.out.print( " " );
}
for (b = 0 ; b <= a; b++) {
System.out.print( "* " );
}
System.out.println();
}
}
public static void main(String args[])
{
int k = 5 ;
StarleftTriangle(k);
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(k2), where k represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Using Recursion
Java
import java.io.*;
class GFG
{
public static void printRow( int n, int totalRows)
{
if (totalRows == 0 )
{
return ;
}
if (totalRows <= n)
{
System.out.print( " *" );
}
else
{
System.out.print( " " );
}
printRow(n, totalRows - 1 );
}
public static void newRow( int n, int totalRows)
{
if (n == 0 )
{
return ;
}
newRow(n - 1 , totalRows);
printRow(n, totalRows);
System.out.print( "\n" );
}
public static void main (String[] args)
{
GFG.newRow( 5 , 5 );
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(n)
Auxiliary Space: O(n), Due to recursion stack in memory.
Method: Iterative approach
The idea behind this code is to print a left triangle pattern of stars with a given number of rows.
Step-by-step approach to implement this idea:
- Initialize the number of rows to be printed.
- Use a loop to iterate through each row, starting from the first row up to the number of rows specified.
- For each row, use another loop to print the stars. The number of stars to be printed in each row is equal to the row number. For example, in the first row, only one star is printed, and in the second row, two stars are printed, and so on.
- After printing the stars in each row, move to the next line to print the stars in the next row.
The code above follows this approach by using two nested for loops. The outer loop is responsible for iterating through each row, while the inner loop is responsible for printing the stars in each row. The number of stars printed in each row is determined by the value of the outer loop variable i.
Java
public class LeftTrianglePattern {
public static void main(String[] args) {
int numRows = 5 ;
for ( int i = 1 ; i <= numRows; i++) {
for ( int j = 1 ; j <= i; j++) {
System.out.print( "* " );
}
System.out.println();
}
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time Complexity:
The outer loop runs numRows times, while the inner loop runs i times in each iteration of the outer loop. Therefore, the total number of iterations is 1 + 2 + 3 + … + numRows, which is equal to numRows(numRows+1)/2. Therefore, the time complexity of the code is O(numRows^2).
Auxiliary Space:
The code uses a constant amount of auxiliary space to store the values of numRows, i and j. Therefore, the auxiliary space complexity of the code is O(1).
Approach Name: Using nested for loops
Steps:
- Take input n from the user.
- Use two nested for loops to print the left triangle star pattern.
- The outer loop runs from 1 to n and the inner loop runs from 1 to i.
- In each iteration of the inner loop, a star is printed.
- After each row is printed, a new line is printed.
Java
public class LeftTriangleStarPattern {
public static void printLeftTriangle( int n) {
for ( int i = 1 ; i <= n; i++) {
for ( int j = 1 ; j <= i; j++) {
System.out.print( "* " );
}
System.out.println();
}
}
public static void main(String[] args) {
int n = 5 ;
printLeftTriangle(n);
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Approach: Java 8 Stream-based Left Triangle Star Pattern Printer
- Initialize an integer n to the desired number of rows for the left triangle star pattern.
- Use Java’s IntStream to iterate over each row number from 1 to n.
- For each row, use another IntStream to iterate over each column number from 1 to the current row number.
- Map each column number to a string consisting of a single star followed by a space character.
- Collect the mapped strings into a single string using Collectors.joining().
- Use System.out.printf() to print the resulting string with left alignment and a width of n*2 to account for the spacing between stars.
- Repeat steps 3 to 6 for each row number.
- The resulting output will be the left triangle star pattern for n rows.
Java
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class LeftTriangleStarPattern {
public static void main(String[] args)
{
int n = 5 ;
IntStream.range( 1 , n + 1 ).forEach(row -> {
String stars
= IntStream.range( 1 , row + 1 )
.mapToObj(i -> "* " )
.collect(Collectors.joining());
System.out.printf( "%" + n * 2 + "s%n" , stars);
});
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time complexity: O(n2)
Auxiliary space: O(n)
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
05 Apr, 2023
Like Article
Save Article