Given an integer N, print a right half pyramid star pattern with N rows. The first row has 1 star, the second row has 2 stars, and each next row has one more star than the previous row. The Nth row has N stars, and all stars are left aligned.

Examples:
Input: N = 3
Output:
*
**
***Input: N = 5
Output:
*
**
***
****
*****
Try it on GfG Practice
Using Nested Loops – O(N²) Time and O(1) Space
The pattern can be printed using two nested loops, where the outer loop controls the rows and the inner loop prints i stars in the i-th row.
Step-by-step approach:
- Run an outer loop from 1 to N to represent each row.
- For each row i, run an inner loop from 1 to i.
- Print a star (*) during each iteration of the inner loop.
- After printing the stars for the current row, print a newline to move to the next row.
- Repeat this process until all N rows are printed.
using namespace std;
int main()
{
int N = 5;
// Outer loop runs N times, once for each row
for (int i = 1; i <= N; i++) {
// Inner loop prints 'i' stars
for (int j = 1; j <= i; j++) {
cout << "*";
}
cout << "\n";
}
return 0;
}
public class GFG {
public static void main(String[] args)
{
int N = 5;
// Outer loop runs N times, once for each row
for (int i = 1; i <= N; i++) {
// Inner loop prints 'i' stars
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
def main():
N = 5
# Outer loop runs N times, once for each row
for i in range(1, N + 1):
# Inner loop prints 'i' stars
for j in range(1, i + 1):
print("*", end="")
print()
if __name__ == "__main__":
main()
using System;
class GFG
{
static void Main()
{
int N = 5;
// Outer loop runs N times, once for each row
for (int i = 1; i <= N; i++)
{
// Inner loop prints 'i' stars
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
let N = 5;
// Outer loop runs N times, once for each row
for (let i = 1; i <= N; i++) {
// Inner loop prints 'i' stars
for (let j = 1; j <= i; j++) {
process.stdout.write("*");
}
console.log();
}
Output
* ** *** **** *****