Today Learn, How to Creating a Tree Pyramid Pattern in C#

Hello Everyone, 

In this blog post, we will explore how to create a Tree pyramid pattern using C#. 

This simple yet effective example will demonstrate the use of nested loops to generate a symmetrical pyramid of asterisks. We will walk through the code step-by-step, explaining how to manage spaces and stars to achieve the desired pyramid shape.

 Additionally, we will discuss potential modifications, such as changing the pyramid's height and using different characters or symbols. Whether you are a beginner looking to understand the basics of loop constructs or an experienced developer seeking a quick refresher, this tutorial will provide a clear and concise guide to building a tree pyramid in C#. Join us as we dive into this fundamental programming exercise and enhance your understanding of C# looping techniques.

Following Code For Tree Pyramid Pattern . 

using System;
namespace Pyramid
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int space = 85;
            int n = 5;

//1------------------------//

            for (int i = 1; i <= n; i++)
            {
                for (int j = space; j >= i; j--)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= i; k++)
                {
                    Console.Write("*");
                }
                for (int m = 1 + 1; m <= i; m++)
                {
                   Console.Write("*");
                }
                Console.WriteLine();
            }

            //2------------------------//

            for (int i = 3; i <= n+2; i++)
            {
                for (int j = space; j >= i; j--)
                {
                   Console.Write(" ");
                }
                for (int k = i; k >= 1; k--)
                {
                    Console.Write("*");
                }
                for (int k = 2; k <= i; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }

            //3----------------------//

            for (int i = n; i <= n+4; i++)
            {
                for (int j = space; j >= i; j--)
                {
                    Console.Write(" ");
                }
                for (int k = i; k >= 1; k--)
                {
                    Console.Write("*");
                }
                for (int m = 2; m <= i; m++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }

            //4-----------------------//

            for (int i = n+2; i <= n+6; i++)
            {
                for (int j = space; j >= i; j--)
                {
                    Console.Write(" ");
                }
                for (int k = i; k >= 1; k--)
                {
                    Console.Write("*");
                }
                for (int m = 2; m <= i; m++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }

            //5-----------------------//

            for (int i = 1; i <= n; i++)
            {
                for (int j =space-2; j >= 1; j--)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= n; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
                
            }
            Console.Read();
        }
    }
    }

Output 





No comments:

Post a Comment