How to print Pyramid Pattern in C#
Description :
There can be many ways of thinking to print pyramid type output. Here I am explaining the easiest one in which I just used only two for loops, one for trailing space and another for equilateral triangle.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace armstrong
{
class Program
{
static void Main(string[] args)
{
int row = 9;
for (int i = 1; i <= row; i++) // Main loop prints 9 rows
{
for (int x = 1; x <= (row - i); x++ ) // for space
{
Console.Write(" ");
}
for (int INC = 1; INC <=i; INC++ )
{
Console.Write(INC);
}
for (int Dec = (i - 1); Dec >= 1; Dec--)
{
Console.Write(Dec);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace armstrong
{
class Program
{
static void Main(string[] args)
{
int row = 9;
for (int i = 1; i <= row; i++) // Main loop prints 9 rows
{
for (int x = 1; x <= (row - i); x++ ) // for space
{
Console.Write(" ");
}
for (int INC = 1; INC <=i; INC++ )
{
Console.Write(INC);
}
for (int Dec = (i - 1); Dec >= 1; Dec--)
{
Console.Write(Dec);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
OutPut :
Second programe for print Star
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace armstrong
{
class Program
{
static void Main(string[] args)
{
int row = 9;
for (int i = 1; i <= row; i++) // Main loop prints 15 rows
{
for (int x = 1; x <= (row - i); x++ ) // for space
{
Console.Write(" ");
}
for (int INC = 1; INC <=i; INC++ )
{
Console.Write("*");
}
for (int Dec = (i - 1); Dec >= 1; Dec--)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace armstrong
{
class Program
{
static void Main(string[] args)
{
int row = 9;
for (int i = 1; i <= row; i++) // Main loop prints 15 rows
{
for (int x = 1; x <= (row - i); x++ ) // for space
{
Console.Write(" ");
}
for (int INC = 1; INC <=i; INC++ )
{
Console.Write("*");
}
for (int Dec = (i - 1); Dec >= 1; Dec--)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
See Other Tutorial :
* AngularJS CRUD Operation : Select Insert Edit Update and Delete using AngularJS in ASP.Net MVC
* AngularJS With ASP.NET MVC
* Convert Rows to columns using 'Pivot' in SQL Server
* Mvc Registration page With user exist using Ajax method
* MVC 4 How to Perform Insert Update Delete Edit Select Operation
* MVC4 Edit,update,Delete,cancel inside gridview using sql database
* MVC 4 Gridview To Display Data Using SQL Server Database With Simple code
* Login page in asp.net Mvc4 Web application
* Mvc4 How to bind Dropdown List using Sql Database
* Gridview find control in asp.net
Sign up here with your email
ConversionConversion EmoticonEmoticon