How to Create Triangle in C#
1>Here we can use different technique to create different triangle shape using pattern '*'
using System;
namespace Test_Application
{
class Program
{
static void Main(string[] args)
{
int val = 5;
int i, j;
for (i = 1; i <= val; i++)
{
for (j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
Output:-
*
**
***
****
*****
----------------------------------------------------------
2> Reverse
using System;
namespace Test_Application
{
class Program
{
static void Main(string[] args)
{
int val = 1;
int i, j;
for (i = 5; i >= val; i--)
{
for (j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
Output:-
*****
****
***
**
*
----------------------------------------------------------
3> RHS
using System;
namespace Test_Application
{
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
*
**
***
****
*****
------------------------------------------------------------------
4> Print 0 and 1 in Traingle form
using System;
namespace Test_Application
{
class Program
{
static void Main(string[] args)
{
int x , y = 0, z;
Console.WriteLine("Enter the Number : ");
z = int.Parse(Console.ReadLine());
for (int i = 1; i <= z; i++)
{
for (x = 1; x <= i; x++)
{
if (y == 1)
{
Console.Write("0");
y = 0;
}
else if (y == 0)
{
Console.Write("1");
y = 1;
}
}
Console.Write("\n");
}
Console.ReadLine();
}
}
}
1>Here we can use different technique to create different triangle shape using pattern '*'
using System;
namespace Test_Application
{
class Program
{
static void Main(string[] args)
{
int val = 5;
int i, j;
for (i = 1; i <= val; i++)
{
for (j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
Output:-
*
**
***
****
*****
----------------------------------------------------------
2> Reverse
using System;
namespace Test_Application
{
class Program
{
static void Main(string[] args)
{
int val = 1;
int i, j;
for (i = 5; i >= val; i--)
{
for (j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
Output:-
*****
****
***
**
*
----------------------------------------------------------
3> RHS
using System;
namespace Test_Application
{
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
*
**
***
****
*****
------------------------------------------------------------------
4> Print 0 and 1 in Traingle form
using System;
namespace Test_Application
{
class Program
{
static void Main(string[] args)
{
int x , y = 0, z;
Console.WriteLine("Enter the Number : ");
z = int.Parse(Console.ReadLine());
for (int i = 1; i <= z; i++)
{
for (x = 1; x <= i; x++)
{
if (y == 1)
{
Console.Write("0");
y = 0;
}
else if (y == 0)
{
Console.Write("1");
y = 1;
}
}
Console.Write("\n");
}
Console.ReadLine();
}
}
}
Output:- Enter the Number : 4
1
01
010
1010