C# Star Pattern Programs

Beginner
6 min read
Updated: Nov 2026
3 examples
Write + WriteLine

Definition and Usage

A C# star pattern is a console program that prints '*' in rows. The outer for loop chooses the row. The inner loop prints each star with Console.Write, then Console.WriteLine ends that row.

'*' + 1 is an int. The char is promoted before the addition, so Console.Write('*' + 1) writes 43, not another star. (char)('*' + 1) is +, the character whose code is 43. "*" + "*" is the string "**", because + concatenates when one operand is a string. "*" + 1 is "*1".

Console.Write('*') stays on the row.

It writes the character and does not end the line. Call Console.WriteLine() once after the inner loop. new string('*', n) builds a string of n stars, and WriteLine can print that whole row in one call.

C# Syntax

The inner loop runs once per star. Write keeps those stars on one line, and WriteLine comes after that loop.

C#
for (int row = 1; row <= rows; row++)
{
    for (int col = 1; col <= row; col++)
        Console.Write('*');   // same row
    Console.WriteLine();      // end the row
}

How C# Prints a Star

Map each part of the console shape to a C# value or call.

PieceC#What it does
StarConsole.Write('*')Writes the character and stays on the row
Run of starsnew string('*', n)A string of n stars
Indentnew string(' ', n)n spaces
Char +'*' + 1The int 43
Cast(char)('*' + 1)The character +
String +"*" + 1The string "*1"
New rowConsole.WriteLine()Ends the row
Edge onlyWrite(col == 1 || col == width ? '*' : ' ')A hollow row

C# Examples

Three programs. Each one changes only the C# that chooses how many stars to print, and where the spaces go.

Example 1 — One star per inner-loop pass

Row 1 prints one star. Each later row prints one more, because the inner loop stops at row.

C#
using System;

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

        for (int row = 1; row <= rows; row++)
        {
            for (int col = 1; col <= row; col++)
                Console.Write('*');
            Console.WriteLine();
        }
    }
}

How It Works

  1. The outer loop walks row from 1 to 5.
  2. Console.Write('*') writes the character and stays on the row.
  3. Console.WriteLine() after the inner loop starts the next row.

Example 2 — Print the whole row with new string

Count row down from 5. One WriteLine writes every star and the newline.

C#
using System;

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

        for (int row = rows; row >= 1; row--)
            Console.WriteLine(new string('*', row));
    }
}

How It Works

  1. new string('*', row) fills a string with that many stars.
  2. WriteLine writes that string and ends the row, so the inner loop is not needed.
  3. row-- makes each later line one star shorter.

Example 3 — Join the spaces and the stars

new string(' ', n) builds the indent. new string('*', n) builds an odd run of stars. + joins the two strings.

C#
using System;

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

        for (int row = 1; row <= rows; row++)
        {
            string spaces = new string(' ', rows - row);
            string stars = new string('*', 2 * row - 1);
            Console.WriteLine(spaces + stars);
        }
    }
}

How It Works

  1. Row 1 builds 4 spaces and 1 star. Row 5 builds 0 spaces and 9 stars.
  2. + concatenates two strings. '*' + 1 would be the int 43.
  3. WriteLine writes the joined line and ends the row.

11 C# Star Pattern Programs

Each card is a full C# program: nested for loops, Console.Write('*') or new string('*', n), and WriteLine. Open one for the walkthrough.

Frequently Asked Questions

In C#, '*' + 1 is an int. The char is promoted before the addition, so the sum is 43 and Write uses the int overload. Console.Write('*') writes the character. (char)('*' + 1) is +, not a second star. "*" + 1 is the string "*1" because one operand is a string.
Console.Write('*') stays on the same row. Console.WriteLine() ends the row. Call WriteLine once after the inner loop. If WriteLine sits inside the inner loop, each star lands on its own line.
Yes, when the row is a solid run of stars. Console.WriteLine(new string('*', row)) writes that row and the newline in one call. Use the inner loop when the row mixes spaces and stars, or when only the edges should be stars.
Console.WriteLine(new string(' ', rows - row) + new string('*', 2 * row - 1)) builds the indent and the stars as one string. WriteLine adds the newline. Row 1 has one star; each later row adds two.

Next: Star Pattern 1

A full C# walkthrough of the right-angled star triangle, from Console.Write('*') through WriteLine.

Star Pattern 1 →

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.

28 people found this page helpful