C# Right-Aligned Triangle Star Pattern

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A right-aligned right-angled triangle keeps the same star counts as Program 1, but adds leading spaces so the right edge stays flush: row i prints rows - i spaces, then i stars.

Remember
Rule: on row i, print (rows - i) spaces, then i stars

    *     ← 4 spaces + 1 star
   **
  ***
 ****
*****     ← 0 spaces + 5 stars (5 rows)

In C# you use one outer loop for the row and two inner loops: one for spaces, one for stars, then Console.WriteLine() to move down. Every line is exactly rows characters wide. That space loop is the usual step toward centered pyramids.

How to Solve It

Two ways to emit the same shape — start with nested loops, then optionally shorten with new string.

MethodIdeaBest for
Two inner loopsSpaces via Write(" "), then stars via Write("*")Learning, interviews, exams
new stringBuild padding and stars as stringsShorter demos once formulas click

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to (rows - i):
        print " " (no newline)
    for k from 1 to i:
        print "*" (no newline)
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Leading spacesfor (j = 1; j <= rows - i; j++) Console.Write(" ");
Print i starsfor (k = 1; k <= i; k++) Console.Write("*");
End the rowConsole.WriteLine();
Width check(rows - i) + i == rows
String shortcutWrite(new string(' ', rows - i)); WriteLine(new string('*', i));
Invert laterGrow spaces, shrink stars → Program 4

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach space and each *
Console.WriteLineEnds the current lineAfter both inner loops

Live Preview

Change the row count and the right-aligned triangle updates instantly — including star total and line width.

Whole numbers from 1 to 20. Tap a chip or type a value — each line is that many characters wide.

Live result 5 rows · 15 stars · width 5
    *
   **
  ***
 ****
*****

Worked Walkthrough — rows = 4

Trace spaces, stars, and total width for each outer-loop value of i.

iSpaces rows - iStarsWidthPrinted row
1314*
2224**
3134***
4044****

Every row has width 4. Star total: 1 + 2 + 3 + 4 = 10 = 4×5/2. Character prints are still O(n²).

C# Programs

Three complete programs: fixed rows, console input, and a new string shortcut. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — space loop first, then star loop.

C#
using System;

class Program
{
    static void Main()
    {
        int rows = 5;

        for (int i = 1; i <= rows; i++)
        {
            for (int j = 1; j <= rows - i; j++)
            {
                Console.Write(" ");
            }
            for (int k = 1; k <= i; k++)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }
    }
}

How It Works

1. Set height. rows = 5 means five lines, each five characters wide.

2. Outer loop picks the row. i runs from 1 to rows.

3. First inner loop prints spaces. j runs from 1 to rows - i, so row i gets rows - i leading spaces.

4. Second inner loop prints stars. k runs from 1 to i — same star counts as Program 1.

5. Break the line. Console.WriteLine() after both inner loops starts the next row.

When i = 1 you get 4 spaces and *; when i = 5 you get 0 spaces and five stars.

Example 2 — User Input Version

Read the row count at runtime. Prefer int.TryParse in real apps (shown in the tip below).

C#
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter the number of rows: ");
        int rows = Convert.ToInt32(Console.ReadLine());

        for (int i = 1; i <= rows; i++)
        {
            for (int j = 1; j <= rows - i; j++)
            {
                Console.Write(" ");
            }
            for (int k = 1; k <= i; k++)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }
    }
}

How It Works

1. Prompt and read. Ask for a row count, then convert the line to an int.

2. Same space/star core. Only the source of rows changes — the print logic matches Example 1.

3. Safer input tip. Convert.ToInt32 throws on letters or empty input. Prefer:

Safer input
if (!int.TryParse(Console.ReadLine(), out int rows) || rows < 1)
{
    Console.WriteLine("Enter a positive whole number.");
    return;
}

Example 3 — new string for Spaces and Stars

Build each row’s padding and star run in one call each — same shape, no explicit character loops.

C#
using System;

class Program
{
    static void Main()
    {
        int rows = 5;

        for (int i = 1; i <= rows; i++)
        {
            Console.Write(new string(' ', rows - i));
            Console.WriteLine(new string('*', i));
        }
    }
}

How It Works

1. One outer loop. Still walk i from 1 to rows.

2. Build the padding. new string(' ', rows - i) creates the leading spaces (empty when i == rows).

3. Build and print stars. WriteLine(new string('*', i)) prints i stars and ends the line.

Learn the two-loop version first (Examples 1–2) so you can explain both bounds in an interview; treat this as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

No spaces

Looks like Program 1

Skipping the space loop reprints the left-aligned triangle. Print rows - i spaces before the stars.

Swapped bounds

Broken right edge

Spaces must be rows - i and stars i. Swapping them loses the flush right edge.

j < rows - i

Off-by-one padding

Use j <= rows - i. A strict < drops one needed space on most rows.

Tabs

Use real spaces

Print " ", not tabs — tabs break alignment across fonts and editors.

rows = 1

Single star

0 spaces + 1 star — same as Program 1 for n = 1.

rows ≤ 0

Empty output

Outer loop never runs. Validate and re-prompt for interactive programs.

Bad input

Use TryParse

Convert.ToInt32 throws on letters — prefer int.TryParse.

Time and Space Complexity

ProgramTimeExtra space
Nested space/star loops (Examples 1–2)O(rows²)O(1)
new string (Example 3)O(rows²)O(rows) per temporary row string

Each of n rows prints Θ(n) characters (spaces + stars). Star count alone is still n(n+1)/2.

Key Takeaways

  • Rule: row i prints rows - i spaces, then i stars.
  • Two inner loops: padding first, then stars with Console.Write.
  • Width check: every row has length rows before WriteLine.
  • Complexity: O(n²) time; O(1) extra space for nested loops.

One line: for each row i, print rows - i spaces, then i stars, then WriteLine.

Frequently Asked Questions

The outer loop runs i from 1 to rows. For each row i, print (rows - i) spaces, then i stars. Row 1 has the most padding and one star; the last row has no spaces and rows stars.
Spaces and stars follow different formulas. One loop prints spaces from 1 to rows minus i; another prints stars from 1 to i. Without the space loop, output stays left-aligned like Program 1.
Console.Write stays on the same line. Console.WriteLine ends the current line. Spaces and stars use Write; the row break uses WriteLine after both inner loops.
Program 1 prints only i stars per row. Program 3 prints (rows - i) spaces first, then i stars, so the same star counts sit flush on the right.
O(n²) where n is the number of rows. Each of n rows prints about n characters (spaces plus stars).
Yes. Console.Write(new string(' ', rows - i)) then Console.WriteLine(new string('*', i)) builds each row without explicit inner character loops.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
The right edge no longer stays flush. Keep spaces = rows - i and stars = i.

Did you know?

Right-aligned and left-aligned triangles use the same star counts per row; only leading spaces change. Each row prints exactly rows characters before the newline: (rows - i) + i = rows.

Next: Inverted Right-Aligned Triangle

Keep the right edge flush while shrinking the star count each row.

Program 4 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful