Blog

Creating Pyramid in C#

Posted by Rohatash Kumar Blogs | C# Language 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

post comment
     

tnks..

Posted by Rajeev Kumar Jan 21, 2013

How can reverse theOutput

Posted by MOHAMED MAHMOUD YASSIN Jan 18, 2013

Thanks. Mahi

Posted by Rohatash Kumar Oct 17, 2012

Very Nice Explation sir, How To make Adjacency matrix in C#.

Posted by mahi singh Oct 17, 2012

Thanks. Richa

Posted by Rohatash Kumar Oct 17, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts