Draw Pattern User Define Input With Optimal Loop Solution In C#

in this artical, learn how to draw patten in c# with optimal loop solution.

For Example if user input the 5 number than draw the patten this user input accordingly.

Output sould be always the based on the user input.

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = Convert.ToInt32(Console.ReadLine());
            for (int row = 1; row <= number; ++row)
            {
                for (int col = 1; col <= row; ++col)
                {
                    Console.Write("*");
                }

                Console.WriteLine();
            }
            for (int row = 1; row <= number; ++row)
            {
                for (int col = 1; col <= row; ++col)
                {
                    Console.Write("{0}",col);
                }

                Console.WriteLine();
            }
            Console.ReadLine();

            for (int row = number; row >= 1; --row)
            {
                for (int col = 1; col <= row; ++col)
                {
                    Console.Write("*");
                }

                Console.WriteLine();
            }
            for (int row = number; row >= 1; --row)
            {
                for (int col = 1; col <= row; ++col)
                {
                    Console.Write("{0}",col);
                }

                Console.WriteLine();
            }
            Console.ReadLine();

            for (int row = 1; row <= number; ++row)
            {
                for (int col = 1; col <= row; ++col)
                {
                    Console.Write("*");
                }

                Console.WriteLine();
            }
            for (int row = 1; row <= number; ++row)
            {
                for (int col = 1; col <= row; ++col)
                {
                    Console.Write("{0}",col);
                }

                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

Please see your output is look like this.

Submit a Comment

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

Subscribe

Select Categories