Creating Pyramid in C#

By Rohatash Kumar Oct 16, 2012
In this blog, we will see how to display a pyramid in C#.

In this blog, we display a pyramid. Pyramid which looks like this:

       1
     121
   12321
 1234321
123454321
 

Solution:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication59

{

    class Program

    {

        static void Main(string[] args)

        {

            int numberoflayer = 6, Space, Number;

            Console.WriteLine("Print paramid");

            for (int i = 1; i <= numberoflayer; i++) // Total number of layer for pramid

            {

                for (Space = 1; Space <= (numberoflayer - i); Space++)  // Loop For Space

                    Console.Write(" ");

                for (Number = 1; Number <= i; Number++) //increase the value

                    Console.Write(Number);

                for (Number = (i - 1); Number >= 1; Number--)  //decrease the value

                    Console.Write(Number);

                Console.WriteLine();

            }

        }

    }

}

 

Paramid-in-csharp.jpg

 

If we want to display asterisk pyramid like this:

 

       *
     ***
    ***
**
  *******
 *********

 

Solution:

 

int numberoflayer = 6, Space, Number;

Console.WriteLine("Print paramid");

for (int i = 1; i <= numberoflayer; i++) // Total number of layer for pramid

{

    for (Space = 1; Space <= (numberoflayer - i); Space++)  // Loop For Space

         Console.Write(" ");

    for (Number = 1; Number <= i; Number++) //increase the value

         Console.Write('*');

    for (Number = (i - 1); Number >= 1; Number--)  //decrease the value

         Console.Write('*');

         Console.WriteLine();

}
 

Asterisk-Paramid-in-csharp.jpg

Rohatash Kumar

Normal 0 false false false EN-AU X-NONE X-NONE I am a Microsoft .NET software Developer and author and C# Corner MVP. I hold Masters degree i... Read more

COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs