Introduction
Pattern printing is one of the best ways to understand loops, nested iterations, and conditional logic in C#.
In this article, we’ll create a Diamond Star Pattern using ASP.NET WebForms, which combines both the Pyramid and Inverted Pyramid logic.
Pattern to Print
If the user enters 5, the output will look like this:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
C# WebForms Real-Time Example
ASPX Page (DiamondPattern.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DiamondPattern.aspx.cs" Inherits="WebApp.DiamondPattern" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Diamond Star Pattern in C# WebForms</title>
</head>
<body>
<form id="form1" runat="server">
<div style="font-family: Arial; margin: 50px;">
<h2>Diamond 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 (DiamondPattern.aspx.cs)
using System;
using System.Text;
namespace WebApp
{
public partial class DiamondPattern : 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 = GenerateDiamond(rows);
}
else
{
lblResult.Text = "Please enter a valid positive number.";
}
}
private string GenerateDiamond(int rows)
{
StringBuilder pattern = new StringBuilder();
// Upper pyramid
for (int i = 1; i <= rows; i++)
{
// Print 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/>");
}
// Lower inverted pyramid
for (int i = rows - 1; i >= 1; i--)
{
// Print spaces
for (int j = rows; j > i; 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 a number of rows in the textbox (e.g., 5).
Button Click Event
When the “Generate Pattern” button is clicked, the program calls GenerateDiamond() method.
Logic Breakdown
The first loop (Upper Pyramid) prints stars increasing from 1 → N.
The second loop (Lower Pyramid) prints stars decreasing from N-1 → 1.
adds spacing in HTML.
<br/> adds line breaks for each row.
Output Display
The full diamond shape is displayed in the lblResult label using HTML formatting.
Example Output (for input 5)
*
***
*****
*******
*********
*******
*****
***
*
(Spaces appear correctly in browser due to .)
Key Concepts
Nested Loops: Used to control the number of spaces and stars.
StringBuilder: Efficient for string concatenation in loops.
HTML Encoding: used for spacing since browsers ignore multiple spaces.
Real-Time Usage
Often used in educational web applications for teaching loop concepts.
Helps beginners visualize patterns and control flow in C#.
Can be used in coding challenge portals built using ASP.NET.
Conclusion
This program demonstrates how to use nested loops, HTML formatting, and recursion-like logic flow to generate a Diamond Star Pattern dynamically in C# WebForms.
It’s a perfect learning project to understand loop structures and pattern printing in ASP.NET.