C# Pattern Programs

This article explains various star (*) pattern programs in c# programming language. You can download the complete project from here containing source code for all the star pattern programs discussed in this article.

Content

Inverted Right Angle Triangle Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	for (int row = 8; row >= 1; --row)
 6         {
 7             for (int col = 1; col <= row; ++col)
 8             {
 9                 Console.Write("*");
10             }
11 
12             Console.WriteLine();
13         }
14         Console.ReadLine();
15     }
16 }        

Right Angle Triangle Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	for (int row = 1; row <= 8; ++row)
 6         {
 7             for (int col = 1; col <= row; ++col)
 8             {
 9                 Console.Write("*");
10             }
11 
12             Console.WriteLine();
13         }
14         Console.ReadLine();
15     }
16 }      

Diamond Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	int number = 8, count = 1;
 6         count = number - 1;
 7         for (var k = 1; k <= number; k++)
 8         {
 9             for (var i = 1; i <= count; i++)
10                 Console.Write(" ");
11             count--;
12             for (var i = 1; i <= 2 * k - 1; i++)
13                 Console.Write("*");
14             Console.WriteLine();
15         }
16         count = 1;
17         for (var k = 1; k <= number - 1; k++)
18         {
19             for (var i = 1; i <= count; i++)
20                 Console.Write(" ");
21             count++;
22             for (var i = 1; i <= 2 * (number - k) - 1; i++)
23                 Console.Write("*");
24             Console.WriteLine();
25         }
26         Console.ReadLine();
27     }
28 }      

Right Angle Triangle Reflection Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	int val = 8;
 6         int i, j, k;
 7         for (i = 1; i <= val; i++)
 8         {
 9             for (j = 1; j <= val - i; j++)
10             {
11                 Console.Write(" ");
12             }
13             for (k = 1; k <= i; k++)
14             {
15                 Console.Write("*");
16             }
17             Console.WriteLine("");
18         }
19         Console.ReadLine();
20     }
21 }     

Parallelogram Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	int size = 8;
 6         for (int row = 1; row <= size; ++row)
 7         {
 8             for (int col = 1; col <= row; ++col)
 9             {
10                 Console.Write(" ");
11             }
12 
13             for (int col = 1; col <= size; ++col)
14             {
15                 Console.Write("*");
16             }
17 
18             Console.WriteLine();
19         }
20         Console.ReadLine();
21     }
22 }         

Hollow Rectangle Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	int number = 7;
 6         for (int i = 0; i < number; i++)
 7         {
 8             if (i == 0 || i == 6)
 9             {
10                 for (int j = 0; j < number; j++)
11                 {
12                     Console.Write("*");
13                 }
14                 Console.WriteLine();
15             }
16             if (i >= 1 && i <= 5)
17             {
18                 for (int j = 0; j < number; j++)
19                 {
20                     if (j == 0 || j == 6)
21                     {
22                         Console.Write("*");
23                     }
24                     else if (j >= 1 && j <= 5)
25                     {
26                         Console.Write(" ");
27                     }
28                 }
29                 Console.WriteLine();
30             }
31         }
32         Console.ReadLine();
33     }
34 }         

Castle Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	int n = 8;
 6         for (int i = 0; i < n; ++i)
 7         {
 8             Stars(i + 1);
 9             Spaces(n - i - 1);
10             Stars(n - i + 1);
11             Spaces(2 * i);
12             Stars(n - i);
13             Spaces(n - i - 1);
14             Stars(i + 1);
15 
16             Console.WriteLine();
17         }
18         Console.ReadLine();
19     }
20 
21     private static void Stars(int count)
22 	{
23 	    for (int i = 0; i < count; ++i)
24 	        Console.Write("*");
25 	}
26 
27 	private static void Spaces(int count)
28 	{
29 	    for (int i = 0; i < count; ++i)
30 	        Console.Write(" ");
31 	}
32 }         

Pyramid Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	int x = 8;
 6         for (int i = 1; i <= x; i++)
 7         {
 8             for (int j = 1; j <= (x - i); j++)
 9                 Console.Write(" ");
10 
11             for (int t = 1; t < i * 2; t++)
12                 Console.Write("*");
13             Console.WriteLine();
14         }
15         Console.ReadLine();
16     }
17 }         

Fair Flag Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	int space = 0;
 6         int max = 10;
 7 
 8         for (var i = max; i > 0; i--)
 9         {
10             for (var j = 0; j < i; j++)
11             {
12                 Console.Write("*");
13             }
14             for (var j = 0; j < space; j++)
15             {
16                 Console.Write(" ");
17             }
18 
19             for (var j = 0; j < i; j++)
20             {
21                 Console.Write("*");
22             }
23 
24             Console.WriteLine();
25             space += 2;
26         }
27         Console.ReadLine();
28     }
29 }         

Hollow Right Angle Triangle Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	int max = 10;
 6         for (var i = 1; i <= max; i++)
 7         {
 8             for (var j = 1; j <= i; j++)
 9             {
10                 if (j == 1 || j == i || i == max)
11                 {
12                     Console.Write("*");
13                 }
14                 else
15                 {
16                     Console.Write(" ");
17                 }
18             }
19             Console.WriteLine();
20         }
21         Console.ReadLine();
22     }
23 }         

Hollow Pyramid Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {
 5         
 6     	int max = 8;
 7         for (var i = 1; i <= max; i++)
 8         {
 9             for (var j = i; j < max; j++)
10             {
11                 Console.Write(" ");
12             }
13             for (var j = 1; j <= (2 * i - 1); j++)
14             {
15                 if (i == max || j == 1 || j == (2 * i - 1))
16                 {
17                     Console.Write("*");
18                 }
19                 else
20                 {
21                     Console.Write(" ");
22                 }
23             }
24             Console.WriteLine();
25         }
26         Console.ReadLine();
27     }
28 }         

Left Arrow Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	var num = 8;
 6         for (var i = -num; i <= num; i++)
 7         {
 8             var k = i;
 9             if (k < 0)
10             {
11                 k = k * -1;
12             }
13             for (var j = 0; j <= num; ++j)
14             {
15                 if (j < k)
16                 {
17                     Console.Write("  ");
18                 }
19                 else
20                 {
21                     Console.Write("* ");
22                 }
23             }
24             Console.WriteLine();
25         }
26         Console.ReadLine();
27     }
28 }         

Hollow Diamond Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	int size = 5;
 6         int z = 1;
 7         for (int i = 0; i <= size; i++)
 8         {
 9             for (int j = size; j > i; j--)
10             {
11                 Console.Write(" ");
12             }
13 
14             Console.Write("*");
15 
16             if (i > 0)
17             {
18                 for (int k = 1; k <= z; k++)
19                 {
20                     Console.Write(" ");
21                 }
22                 z += 2;
23                 Console.Write("*");
24             }
25             Console.WriteLine();
26         }
27 
28         z -= 4;
29 
30         for (int i = 0; i <= size - 1; i++)
31         {
32             for (int j = 0; j <= i; j++)
33             {
34                 Console.Write(" ");
35             }
36 
37             Console.Write("*");
38 
39             for (int k = 1; k <= z; k++)
40             {
41                 Console.Write(" ");
42             }
43             z -= 2;
44 
45             if (i != size - 1)
46             {
47                 Console.Write("*");
48             }
49             Console.WriteLine();
50         }
51         Console.ReadLine();
52     }
53 }         

Heart Pattern

 1 class Program
 2 {
 3     static void Main(string[] args)
 4     {        
 5     	int size = 5;
 6         for (int x = 0; x < size; x++)
 7         {
 8             for (int y = 0; y <= 4 * size; y++)
 9             {
10                 double dist1 = Math.Sqrt(Math.Pow(x - size, 2) + Math.Pow(y - size, 2));
11                 double dist2 = Math.Sqrt(Math.Pow(x - size, 2) + Math.Pow(y - 3 * size, 2));
12 
13                 if (dist1 < size + 0.5 || dist2 < size + 0.5)
14                     Console.Write("*");
15                 else
16                     Console.Write(" ");
17             }
18             Console.WriteLine();
19         }
20 
21         for (int x = 1; x < 2 * size; x++)
22         {
23             for (int y = 0; y < x; y++)
24                 Console.Write(" ");
25 
26             for (int y = 0; y < 4 * size + 1 - 2 * x; y++)
27             {
28                 Console.Write("*");
29 
30             }
31             Console.WriteLine();
32         }
33         Console.ReadLine();
34     }
35 }         
C# Pattern Programs
Share this

Subscribe to Code with Shadman