Introduction
Star patterns are one of the most common exercises for beginners to learn looping and logic control in programming.
In this article, we’ll create a Pyramid Star Pattern using C# WebForms (Visual Studio 2015), demonstrating the use of nested loops and string building for dynamic HTML output.
Pattern to Print
If the user enters 5, output should be:
*
* *
* * *
* * * *
* * * * *
C# WebForms Real-Time Example
ASPX Page (PyramidPattern.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PyramidPattern.aspx.cs" Inherits="WebApp.PyramidPattern" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Pyramid Star Pattern in C# WebForms</title>
</head>
<body>
<form id="form1" runat="server">
<div style="font-family: Arial; margin: 50px;">
<h2>Pyramid Star Pattern</h2>
<asp:Label ID="Label1" runat="server" Text="Enter number of rows: " />
<asp:TextBox ID="txtRows" runat="server"></asp:TextBox>
<asp:Button ID="btnGenerate" runat="server" Text="Generate Pattern" OnClick="btnGenerate_Click" />
<br /><br />
<asp:Label ID="lblResult" runat="server" Font-Names="Consolas" Font-Size="Large"></asp:Label>
</div>
</form>
</body>
</html>
Code-Behind (PyramidPattern.aspx.cs)
using System;
using System.Text;
namespace WebApp
{
public partial class PyramidPattern : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGenerate_Click(object sender, EventArgs e)
{
int rows;
if (int.TryParse(txtRows.Text, out rows) && rows > 0)
{
lblResult.Text = GeneratePyramid(rows);
}
else
{
lblResult.Text = "Please enter a valid positive number.";
}
}
private string GeneratePyramid(int rows)
{
StringBuilder pattern = new StringBuilder();
for (int i = 1; i <= rows; i++)
{
// Print leading spaces
for (int j = i; j < rows; j++)
{
pattern.Append(" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++)
{
pattern.Append("*");
}
pattern.Append("<br/>");
}
return pattern.ToString();
}
}
}
Explanation
User Input:
The user enters the number of rows in the textbox.Button Click:
The methodbtnGenerate_Clicktriggers the pattern generation.Logic Used:
Outer Loop (
i) controls the number of rows.First Inner Loop (
j) adds spaces to center the stars.Second Inner Loop (
k) prints stars based on the current row.
is used for spacing (since HTML collapses spaces).<br/>is used for line breaks to display the pyramid vertically.
Example Output
If you enter 5, you’ll get:
*
***
*****
*******
*********
(Rendered properly in the web browser using spacing.)
Key Concepts
Nested Loops → Used for spacing and star printing.
HTML Rendering → Uses
for spaces and<br/>for new lines.StringBuilder → Efficient for constructing pattern strings in WebForms.
Real-Time Usage
Pyramid pattern generation logic is often used in:
Coding practice exercises in learning portals.
Online programming tutorials.
Educational web-based compilers built using ASP.NET WebForms.
Conclusion
This example shows how to use C# nested loops and string manipulation in a WebForms environment to generate a beautiful Pyramid Star Pattern dynamically based on user input.
It’s a perfect exercise for understanding loops, condition logic, and HTML rendering in ASP.NET.

Join the conversation! Your thoughts help the community grow.