Triangle of Pascal

  • Thread starter J Ivan P Silvestre
  • Start date
J

J Ivan P Silvestre

c #: Implementing the method Int [,] TrianglePascal (int n) which
returns the triangle to

the level of Pascal N.


public static int[,] TrianglePascal(int n)
{
int[,] arr = new int[n+1,n+1];
for(int i=0;i<n+1;i++)
{
for(int k=0;k<n+1;k++)
{
if(k==0)
arr[i,k]=1;
else if(i==k)arr[i,k]=1;
}
}
for(int i=0;i<n+1;i++)
{
for(int k=0;k<n+1+1;k++){
if(i>=1&&i+1<n+1&&k+1<n+1)
{
if(arr[i,k]!=0&&arr[i,k+1]!=0)
arr[i+1,k+1]=arr[i,k]+arr[i,k+1];
}
}
}
return arr;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top