Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Different Star Pattern Programs in C#
Star pattern programs are common programming exercises that help developers practice loops and understand nested iteration logic. C# provides simple syntax to create various star patterns using for loops and Console.Write() methods.
Right Triangle Pattern
This pattern creates a right-angled triangle where each row contains one more star than the previous row −
using System;
class Program {
static void Main() {
for (int i = 1; i
The output of the above code is −
*
**
***
****
*****
******
Left-Aligned Triangle Pattern
This pattern creates a triangle aligned to the left with spaces for proper positioning −
using System;
class Program {
static void Main() {
int n = 6;
for (int i = 1; i
The output of the above code is −
*
**
***
****
*****
******
Inverted Triangle Pattern
This pattern creates an inverted triangle where each row has one less star than the previous row −
using System;
class Program {
static void Main() {
for (int i = 6; i >= 1; --i) {
for (int j = 1; j
The output of the above code is −
******
*****
****
***
**
*
Diamond Pattern
This pattern creates a diamond shape by combining ascending and descending triangular patterns −
using System;
class Demo {
static void Main() {
int rows = 6;
int spaces = rows - 1;
int stars = 1;
for (int i = 1; i
The output of the above code is −
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
How It Works
Star patterns use nested loops where the outer loop controls the number of rows and the inner loop controls the number of stars or spaces in each row. The key concepts are:
Outer loop: Iterates through each row from top to bottom
-
Inner loops:
Control spaces for alignment and stars for the pattern Console.Write(): Prints without a newline, allowing stars to appear on the same row
Console.WriteLine(): Moves to the next row after printing all stars
Conclusion
Star pattern programs in C# demonstrate fundamental programming concepts like nested loops, conditional logic, and output formatting. These exercises help developers understand loop control structures and develop problem-solving skills for creating visual patterns programmatically.
