Triangle Patterns in CSharp.
Triangle: 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class Program
{
Static void Main(string[] args)
{
int val = 5;
int i, j, k ;
for (i = 1; i <= val; i++)
{
for (j = 1; j <= i; j++)
{
Console.Write("");
}
for (k = 1; k <= i; k++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
Or, you can write it, by doing some minor changes,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class Program
{
static void Main(string[] args)
{
int val = 5;
int i, j, k ;
for (i = 1; i <= val; i++)
{
for (j = 1; j <= val-i; j++)
{
// Console.Write("");
}
for (k = 1; k <= i; k++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
Output Window
Triangle: 2 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class Program
{
static void Main(string[] args)
{
int val = 5;
int i, j, k ;
for (i = 1; i <= val; i++)
{
for (j = 1; j <= val-i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= i; k++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
[The way I mentioned for the first triangle, in the same manner you can workout this pattern too, by applying some minor changes:]
Output Window
Triangle: 3 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class Program
{
static void Main(string[] args)
{
int val = 5;
int i, j, k ;
for (i = 1; i <= val; i++)
{
for (j = 1; j <= val-i; j++)
{
// Console.Write(" ");
}
for (k = 1; k <= j; k++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
Output Window