C# program to print Pascal Triangle
Program
using System;
namespace PrintPascalTriangle
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Pascal Triangle");
Console.Write("Enter the number of rows:\t");
int row = int.Parse(Console.ReadLine());
int i, j;
Program prog = new Program();
for (i = 0; i < row; i++)
{
for (j = 0; j <= (row - i - 2); j++)
{
Console.Write(" ");
}
for (j = 0 ; j <= i; j++)
{
int value = prog.fact(i) / (prog.fact(j) * prog.fact(i - j));
Console.Write("{0} ",value);
}
Console.Write("\n");
}
}
int fact(int n)
{
int i;
int result = 1;
for (i = 1; i <= n; i++)
{
result = result * i;
}
return result;
}
}
}
Output
Pascal Triangle
Enter the number of rows: 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1