Last Updated : 10 Apr, 2023
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.*;
// Java code to demonstrate right star triangle
public class GeeksForGeeks {
// Function to demonstrate printing pattern
public static void StarRightTriangle(int n)
{
int a, b;
// outer loop to handle number of rows
// k in this case
for (a = 0; a < n; a++) {
// inner loop to handle number of columns
// values changing acc. to outer loop
for (b = 0; b <= a; b++) {
// printing stars
System.out.print("* ");
}
// end-line
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int k = 5;
StarRightTriangle(k);
}
}
* * * * * * * * * * * * * * *
Time complexity: O(n2) where n is given input.
Auxiliary Space : O(1)
// Contributed by Manish Sharma
import java.io.*;
class GFG
{
public static void printRow(int n) // for printing a row
{
if(n == 0)
{
return;
}
System.out.print("* ");
printRow(n - 1); // for next * in the current row
}
public static void changeRow(int n) // for moving to next row...n = 1 means last row
{
if(n == 0)
{
return;
}
changeRow(n - 1);
printRow(n); // when call stack of changeRow method is popping out we will print row
System.out.print("\n"); // new line after each column
}
public static void main (String[] args)
{
GFG.changeRow(5); // changeRow method is static so no need to create an object of GFG class.
}
}
* * * * * * * * * * * * * * *
Time complexity: O(n2) where n is given input.
Auxiliary Space: O(n2), due to recursion call stack.
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:
public class RightTriangleStarPattern {
public static void main(String[] args) {
int rows = 5; // Input number of rows
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
* * * * * * * * * * * * * * *
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
public class RightTriangle {
public static void main(String[] args) {
int n = 5; // number of rows
int numStars = 1; // number of stars to be printed in each row
for (int i = 1; i <= n; i++) { // loop for rows
for (int j = 1; j <= numStars; j++) { // loop for printing stars in each row
System.out.print("* ");
}
numStars++; // increment the number of stars for the next row
System.out.println(); // move to next line
}
}
}
* * * * * * * * * * * * * * *
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:Steps:
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++;
}
}
}
* ** *** **** *****
Time Complexity: O(n)
Auxiliary Space: O(n)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4