Exploring Star Patterns in CSharp

C# is a powerful language that allows developers to express their creativity through code. One of the fascinating aspects of programming is generating patterns, and star patterns are a classic example. In this article, we will explore various star pattern programs in C# and understand the logic behind each.

Example 1: Right-Angled Triangle Star Pattern

using System;

class Program
{
    static void Main()
    {
        for (int row = 8; row >= 1; --row)
        {
            for (int col = 1; col <= row; ++col)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}

This program creates a right-angled triangle with stars. The outer loop controls the number of stars in each row, decrementing from 8 to 1. The inner loop prints the stars for each row.

Example 2: Inverted Right-Angled Triangle Star Pattern


using System;

class Program
{
    static void Main()
    {
        for (int row = 1; row <= 8; ++row)
        {
            for (int col = 1; col <= row; ++col)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}

This pattern is the reverse of the first example. Here, the outer loop increments from 1 to 8, creating an inverted triangle.

Example 3: Hollow Square Star Pattern


using System;

class Program
{
    static void Main()
    {
        int n = 5;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (i == 1 || i == n || j == 1 || j == n)
                {
                    Console.Write("*");
                }
                else
                {
                    Console.Write(" ");
                }
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}

This code generates a hollow square using stars. It uses conditional statements to determine whether to print a star or a space.

Example 4: Diamond Star Pattern


using System;

class Program
{
    static void Main()
    {
        int n = 5;
        for (int i = 1; i <= n; i++)
        {
            for (int j = n; j > i; j--)
            {
                Console.Write(" ");
            }
            for (int k = 1; k <= (2 * i - 1); k++)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }
        for (int i = n - 1; i >= 1; i--)
        {
            for (int j = n; j > i; j--)
            {
                Console.Write(" ");
            }
            for (int k = 1; k <= (2 * i - 1); k++)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}

This program creates a diamond shape using stars. It consists of two parts: the top half of the diamond and the bottom half, which are mirrored.

Example 6: Half Pyramid Star Pattern


using System;

class Program
{
    static void Main()
    {
        int n = 5;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}

This program creates a half pyramid using stars. The outer loop controls the number of rows, and the inner loop prints the stars for each row.

-

Related Articles
Coming Soon