To write a C# program for a pascal's triangle
C# Compiler or visual studio
Initialize the necessary attributes.
Get the limit from the user.
using for loop print the rows and columns and space.
Check the first and last rows of the triange is 1 using if condition.
Otherwise use else to print the inner value
val = val * (i - j + 1) / j;
Check the program for any error.
if not.
print the program.
Developed by:R.Vijay
Reg.No:212221230121
using System;
namespace Pascal
{
public class program
{
static void Main(string[] args)
{
int rows,i,val=1;
Console.WriteLine("Enter the number: ");
rows = Convert.ToInt32(Console.ReadLine());
for(i=0;i<rows;i++)
{
for (int space = 1; space < rows - i; space++)
{
Console.Write(" ");
}
for(int j=0;j<=i;j++)
{
if (i == 0 || j == 0)
{
val = 1;
}
else
{
val = val * (i - j + 1) / j;
}
Console.Write(val + " ");
}
Console.WriteLine();
}
}
}
}
Thus,To write a C# program for a pascal's triangle is written and executed.