Right-Aligned Right-Angled Triangle Star Pattern in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Spaces + stars

What You'll Learn

How to print a right-aligned right-angled triangle in C#. Each row still has 1, 2, …, rows stars like Program 1, but you print (rows - i) spaces first so the stars shift right and the bottom row lines up on the right.

With rows = 5, the first line has four spaces and one star; the last line has five stars and no leading spaces.

⭐ Pattern Output

When you run the program with rows = 5, you’ll see this output:

Output
    *
   **
  ***
 ****
*****
1

Complete C# Program

Fixed rows = 5 version (spaces then stars):

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 5;
            int i, j, k;

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

🧠 How It Works

1

Setup

rows is the height. i indexes the row; j runs the space loop; k runs the star loop. Braces group each for body under Main.

Setup
2

Outer loop (rows)

for (i = 1; i <= rows; i++) walks from one star on line 1 up to rows stars on the last line.

Row control
3

Padding: Console.Write(" ")

for (j = 1; j <= rows - i; j++) { Console.Write(" "); } prints rows - i spaces so the star block shifts right while the right edge stays straight.

Right align
4

Stars: Console.Write("*")

for (k = 1; k <= i; k++) { Console.Write("*"); } prints exactly i asterisks after the padding—same star count per row as a left-aligned triangle, different horizontal offset.

Stars
5

New line

Console.WriteLine() ends the row (blank overload adds only a newline). Every row has (rows - i) + i = rows characters before the break.

Line break
=

Right-aligned triangle

Star total n(n+1)/2; O(n²) output for n = rows, O(1) extra space. Full-width lines scroll horizontally in the green preview on narrow viewports.

2

Variation — User Input Version

Read rows with Console.ReadLine() and Convert.ToInt32:

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows;
            int i, j, k;

            Console.Write("Enter the number of rows: ");
            rows = Convert.ToInt32(Console.ReadLine());

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

💡 Tips for Enhancement

Try These

  • Validate rows > 0 after parsing input
  • Remove the space loop to recover Program 1
  • Use int.TryParse instead of Convert.ToInt32 for safer console apps
  • Print digits instead of * for a right-aligned number triangle

Avoid

  • Swapping rows - i and i between the two inner loops
  • Off-by-one on j <= rows - i (use <=, not <, for this formulation)
  • Mixing tabs with spaces (alignment breaks across fonts)

Key Takeaways

1

Print (rows - i) spaces, then i stars, each row.

2

Two inner loops handle two different per-row counts (padding vs stars).

3

Star counts match Program 1; only leading padding changes.

4

Time complexity O(n²) for n rows.

5

Typical stepping stone toward centered pyramids and diamonds.

❓ Frequently Asked Questions

Print (rows - i) spaces, then i stars. The spaces push the star run to the right so the longest line is flush on the right.
You need one repetition count for spaces and another for stars. They change differently as i increases.
Program 1 prints only stars. Program 3 adds a space loop first so the same i stars appear shifted right.
O(n²) for n = rows: each row prints Θ(n) characters.

Explore More C# Star Patterns!

Mirrored rows, pyramids, and diamonds all build on spaces plus stars.

All C# Star Patterns →
Did you know?

Right-aligned and left-aligned triangles use the same star counts per row; only leading spaces change. Many courses teach Program 1 first, then add the space loop for this pattern.

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