In this article, we will learn about printing Right Triangle Star Pattern.
Examples:
Input : n = 5
Output:
*
* *
* * *
* * * *
* * * * *
Right Triangle Star Pattern:
Java
import java.io.*;
public class GeeksForGeeks {
public static void StarRightTriangle( int n)
{
int a, b;
for (a = 0 ; a < n; a++) {
for (b = 0 ; b <= a; b++) {
System.out.print( "* " );
}
System.out.println();
}
}
public static void main(String args[])
{
int k = 5 ;
StarRightTriangle(k);
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time complexity: O(n2) where n is given input.
Auxiliary Space : O(1)
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 changeRow( int n)
{
if (n == 0 )
{
return ;
}
changeRow(n - 1 );
printRow(n);
System.out.print( "\n" );
}
public static void main (String[] args)
{
GFG.changeRow( 5 );
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time complexity: O(n2) where n is given input.
Auxiliary Space: O(n2), due to recursion call stack.
Method: Using nested loops
Approach:
The goal of this program is to print a right triangle star pattern using asterisks. We will use a nested for loop to achieve this. The outer loop will be responsible for iterating over each row of the pattern, while the inner loop will print the asterisks on each line.
Step-by-step approach:
- Start by declaring a class named “RightTriangleStarPattern”.
- Inside the class, declare a “main” method.
- Initialize a variable “rows” to store the number of rows to be printed in the pattern. In this example, we will use the value 5.
- Use a “for” loop to iterate over each row of the pattern. Set the initial value of the loop variable “i” to 1 and continue looping while “i” is less than or equal to “rows”.
- Increment “i” by 1 after each iteration.
- Inside the outer loop, use another “for” loop to print the asterisks on each line. Set the initial value of the loop variable “j” to 1 and continue looping while “j” is less than or equal to “i”. Increment “j” by 1 after each iteration.
- Inside the inner loop, print an asterisk followed by a space using the “System.out.print” statement.
- After the inner loop completes, print a newline character using the “System.out.println” statement to move to the next line.
- Run the program to see the output.
Java
public class RightTriangleStarPattern {
public static void main(String[] args) {
int rows = 5 ;
for ( int i = 1 ; i <= rows; i++) {
for ( int j = 1 ; j <= i; j++) {
System.out.print( "* " );
}
System.out.println();
}
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
The time complexity: O(n^2), where n is the number of rows
The auxiliary space complexity: O(1)
Method: Single loop with arithmetic operations
- Set the number of rows you want to print in the triangle (‘n’).
- Initialize a variable to keep track of the number of stars to be printed in each row (‘numStars’) to 1.
- Start a loop that iterates over the rows of the triangle (‘i’).
- Within the loop for each row, start a nested loop that iterates over the columns of that row (‘j’).
- Within the inner loop, print a star (‘*’) for each column of the row.
- Increment the ‘numStars’ variable by 1 after the inner loop is complete so that the next row has one more star than the previous row.
- Print a newline character (‘\n’) after the inner loop to move to the next line and start the next row.
- Repeat steps 4-7 for each row of the triangle.
Java
public class RightTriangle {
public static void main(String[] args) {
int n = 5 ;
int numStars = 1 ;
for ( int i = 1 ; i <= n; i++) {
for ( int j = 1 ; j <= numStars; j++) {
System.out.print( "* " );
}
numStars++;
System.out.println();
}
}
}
|
Output
*
* *
* * *
* * * *
* * * * *
The time complexity is O(n^2), where n is the number of rows
The auxiliary space complexity of this approach is O(1)
Approach:
- Take the input ‘n’.
- Initialize a string variable ‘row’ with a single ‘*’ character.
- Use a while loop that runs n times.
- Print the current value of ‘row’.
- Append another ‘*’ character to the ‘row’ variable.
- Move to the next line using the “\n” character.
Steps:
- Take the input ‘n’.
- Initialize a string variable ‘row’ with a single ‘*’ character.
- Use a while loop that runs n times.
- Print the current value of ‘row’.
- Append another ‘*’ character to the ‘row’ variable.
- Move to the next line using the “\n” character.
Java
public class RightTrianglePattern {
public static void main(String[] args) {
int n = 5 ;
String row = "*" ;
int i = 1 ;
while (i <= n) {
System.out.println(row);
row += "*" ;
i++;
}
}
}
|
Output
*
**
***
****
*****
Time Complexity: O(n)
Auxiliary Space: O(n)
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 :
10 Apr, 2023
Like Article
Save Article