C# Program To Print Diamond Pattern Using Single For Loop

How to print diamond pattern using single for loop in C#


Please Subscribe Youtube| Like Facebook | Follow Twitter

In this tip, we will write C# method that will print diamond pattern using single for loop.

private static void PrintDiamontSingleLoop(int row) 
{
    row++;
    int mid = row/2;
    int midLeft = mid;
    int midRight = mid;
    int currentRow = 1;
    int currentColumn = 1;
    for (int i = 1; i <= row * row; i++) 
    {
               
        if(i % row == 0)
        {
            Console.WriteLine("");

            if(currentRow < mid)
            {
                midLeft--;
                midRight++;

            }else
            { 

                midLeft++;
                midRight--;
            }

            currentColumn = 1;
            currentRow++;
        }

        if (currentColumn >= midLeft && currentColumn <= midRight)
        {

            Console.Write("*");
        }
        else
        {

            Console.Write(" ");

        }
                                  
        currentColumn++;
            
    }
                  
}
Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *