To write a C# Program for Pascal Triangle.
Hardware - PC Software - Microsoft Studio Code (C#)
Import the 'System' namespace to use the classes present in the 'System' namespace.
Declare the Main method.
Declare variable n,i,j,space.
Using " Console.Write " print the statement and get the input from user using " Console.ReadLine " .
Using for loop print the required pattern.
Step 6: Print the result.
using System;
using System.Globalization;
namespace Pattern
{
class Program
{
static void Main(string[] args)
{
int n,i,j,c=1,space;
Console.Write("Enter the No of Rows : ");
n=Convert.ToInt32(Console.ReadLine());
for(i = 0; i < n; i++)
{
for(space=1;space<=n-i;space++)
{
Console.Write(" ");
}
for( j=0 ; j<=i ; j++)
{
if(i==0 || j==0)
{
c = 1;
}
else
{
c = c*(i - j + 1) / j;
}
Console.Write("{0} ",c);
}
Console.WriteLine();
}
}
}
}
Thus a C# Program for Pascal Triangle is written and executed sucessfully.