Simplest Way To Print Star Pattern Using Single Loop In C#

Introduction

Today we will learn how to write simple program to print star pattern using single loop in c#.

 

Pattern A :

public class Program
{
    public static void Main(string[] args)
    {
       string series = string.Empty;
       for (int i = 0; i <= 5; i++)
       {
          series += "*";
          Console.WriteLine(series);
       }
       Console.ReadLine();
    }
}

Pattern B :

public class Program
{  public static void Main(string[] args)
     {
         int n = 6;
         string spaces = new string(' ', n);
         string series = string.Empty;
         for (int i = 0; i < n; i++)
         {
             series += "*";
             Console.WriteLine(spaces + series);
             spaces = spaces.Substring(1);
         }
         Console.ReadLine();
     }
}

Pattern C:

public class Program
{
    public static void Main(string[] args)
    {
        int n = 6;
        string series = new string('*', n);
        string spaces = string.Empty;
        for (int i = 0; i < n; i++)
        {
            Console.WriteLine(spaces + series);
            spaces +=' ';
            series = series.Substring(1);
        }
        Console.ReadLine();
    }
}

Pattern D:

public class Program
{ 
    public static void Main(string[] args)
    {
        int n = 6;
        string series = new string('*', n);
        for (int i = 0; i < n; i++)
        {
            Console.WriteLine(series);
            series = series.Substring(1);
        }
        Console.ReadLine();
    }
}

 

Please do comments if you want more patterns with a single loop. I will try to post them also.

Thanks.

Next Recommended Reading Star pattern in c#