Home  »  Articles  »  C#.Net  »

Generate Numbers in Pyramid format

 Posted By : Manoj SahooPosted Date : 29/10/2010 07:29:07 AM Category : C#.Net Points : 2

Hello,

Below Example gives the out put like :

1

1 2 1

1 2 3 2 1

Code for Console Application :

static void Main(string[] args)
{
      for (int i = 1; i < 4; i++)
      {
            int j = 1;
            for (j = 1; j <= i; j++)
            {
                 Console.Write(j.ToString() + " ");
             }
             for (int k = j - 2; k >= 1; k--)
            {
                Console.Write(k.ToString() + " ");
             }
             Console.WriteLine("");
      }
      Console.ReadLine();
}

Code for ASP.Net :

void CreatePyramid()
    {
        int start = 0;
        for (int i = 1; i < 4; i++)
        {
            int j = 1;
            for (j = 1; j <= i; j++)
            {
                Response.Write(j.ToString() + " ");
            }
            for (int k = j-2; k >= 1; k--)
            {
                Response.Write(k.ToString() + " ");
            }

            Response.Write("<br>");
        }
    }

Thank You.

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.